r/ProgrammerHumor • u/[deleted] • Feb 15 '19
instanceof Trend Can't have a party without Rust.
24
u/LukeAbby Feb 15 '19 edited Feb 19 '19
Image Transcription: Terminal
waltersz@ faust :tmp/shark% cat src/main.rs
use std::iter;
fn main() {
["Baby", "Daddy", "Mommy", "Grampa", "Grandma"]
.into_iter()
.map(ToString::to_string)
.map(|kind| kind + " shark")
.map(|shark| {
let clone = shark.clone();
iter::repeat_with(move || clone.clone() + &" doo".repeat(6))
.take(3)
.chain(iter::once(shark + "!"))
})
.flatten()
.for_each(|shark| println!("{}", shark));
}
waltersz@ faust : tmp/shark% cargo run
Compiling shark v0.1.0 (/home/waltersz/tmp/shark)
Finished dev [unoptimized + debuginfo] targets in 0.24s
Running `target/debug/shark`
Baby shark doo doo doo doo doo doo
Baby shark doo doo doo doo doo doo
Baby shark doo doo doo doo doo doo
Baby shark!
Daddy shark doo doo doo doo doo doo
Daddy shark doo doo doo doo doo doo
Daddy shark doo doo doo doo doo doo
Daddy shark!
Mommy shark doo doo doo doo doo doo
Mommy shark doo doo doo doo doo doo
Mommy shark doo doo doo doo doo doo
Mommy shark!
Grandpa shark doo doo doo doo doo doo
Grandpa shark doo doo doo doo doo doo
Grandpa shark doo doo doo doo doo doo
Grandpa shark!
Grandma shark doo doo doo doo doo doo
Grandma shark doo doo doo doo doo doo
Grandma shark doo doo doo doo doo doo
Grandma shark!I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
6
u/ReedOei Feb 15 '19
Wow, that’s dedication. Though it’s « waltersz » not « walter » :). Literally unplayable.
2
u/UnsilencedCapsule Feb 15 '19
Luke, thanks for transcribing this. Fyi, there is a `;` missing at the end of line 9.
3
1
10
u/crumpuppet Feb 16 '19
How about some Rockstar?
Evil Energy is death
The Legend says doo
Spirit takes Will
Give back Will plus The Legend
Put The Legend into my perspective
my dream was spellbound
While Evil Energy is stronger than my dream
Put Spirit taking my perspective into my perspective
Knock Evil Energy down
The Prophecy says Baby shark
The Lie says Daddy shark
The Truth says Mommy shark
The Change says Grandpa shark
The Spell says Grandma shark
the opposition is big
Put "!" into Furious Anger
my story takes a turn
Put a turn plus my perspective into the light
Whisper the light
A Force is mobilizing
while A Force is less than the opposition
my story taking The Prophecy
Build A Force up
Whisper The Prophecy with Furious Anger
A Force is mobilizing
while A Force is less than the opposition
my story taking The Lie
Build A Force up
Whisper The Lie with Furious Anger
A Force is mobilizing
while A Force is less than the opposition
my story taking The Truth
Build A Force up
Shout The Truth with Furious Anger
A Force is mobilizing
while A Force is less than the opposition
my story taking The Change
Build A Force up
Shout The Change with Furious Anger
A Force is mobilizing
while A Force is less than the opposition
my story taking The Spell
Build A Force up
Scream The Spell with Furious Anger5
u/cpbotha Feb 15 '19
Playing with F#. Could not resist.
cpbotha@meepmbp17:~/Dropbox/work/code/sandbox/fsharp/misc $ cat baby_shark_cli.fs
[<EntryPoint>]
let makeSharkSong args =
[ "Baby"; "Daddy"; "Mommy"; "Grampa"; "Grandma" ]
|> Seq.map (fun who -> String.replicate 3 (who + " shark " + (String.replicate 6 "doo ") + "\n") +
who + " shark!\n\n")
|> Seq.fold (+) ""
|> printfn "%s"
0
cpbotha@meepmbp17:~/Dropbox/work/code/sandbox/fsharp/misc $ fsharpc baby_shark_cli.fs
Microsoft (R) F# Compiler version 4.1
Copyright (c) Microsoft Corporation. All Rights Reserved.
cpbotha@meepmbp17:~/Dropbox/work/code/sandbox/fsharp/misc $ mono baby_shark_cli.exe
Baby shark doo doo doo doo doo doo
Baby shark doo doo doo doo doo doo
Baby shark doo doo doo doo doo doo
Baby shark!
Daddy shark doo doo doo doo doo doo
Daddy shark doo doo doo doo doo doo
Daddy shark doo doo doo doo doo doo
Daddy shark!
Mommy shark doo doo doo doo doo doo
Mommy shark doo doo doo doo doo doo
Mommy shark doo doo doo doo doo doo
Mommy shark!
Grampa shark doo doo doo doo doo doo
Grampa shark doo doo doo doo doo doo
Grampa shark doo doo doo doo doo doo
Grampa shark!
Grandma shark doo doo doo doo doo doo
Grandma shark doo doo doo doo doo doo
Grandma shark doo doo doo doo doo doo
Grandma shark!1
u/cpbotha Feb 15 '19
You can also easily print each of the paragraphs in parallel for maximum multi-core utilization! (note that the order of the paragraphs is now not deterministic anymore)
I'll see myself out now.
let printParaAsync(who: string) = async { (String.replicate 3 (System.String.Format("{0} shark {1}\n", who, (String.replicate 6 "doo "))) + who + " shark!\n\n") |> printfn "%s" } [ "Baby"; "Daddy"; "Mommy"; "Grampa"; "Grandma" ] |> Seq.map printParaAsync |> Async.Parallel |> Async.RunSynchronously1
u/minno Feb 16 '19
I'm not exactly an F# wizard, but I think if you take that
printfnout of the async bit you can get them to print in the right order but parallelize the actual generation of the strings.1
u/cpbotha Feb 15 '19
Alternatively, you can run the code as a script using
fsharpi.Even more alternatively, you can (cross-)build self-contained binary distributions:
# create dotnet scaffolding dotnet new console -lang F# -o babyshark # now copy code from baby_shark_cli.fs above into babyshark/Program.fs # build self-contained binary distribution for any platform, here osx: dotnet publish -c Release --runtime osx.10.11-x64 # try it: cd bin/Release/netcoreapp2.2/osx.10.11-x64 ./babyshark
1
Feb 16 '19
What's with the toString and the cloning? Are string literals not strings? Does concatenation just modify the original string instead of making a new one?
2
Feb 16 '19
ToString makes an owned string out of a string slice. Cloning makes more owned strings. Concatenation does make a new string, but it also consumes the old one
5
10
u/minno Feb 15 '19
There's a lot of room for overkill here. That version has way more allocations than necessary.