Skip to content

Instantly share code, notes, and snippets.

@julienba
Forked from ahoy-jon/MiniRosetta.md
Last active August 29, 2015 13:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julienba/10681841 to your computer and use it in GitHub Desktop.
Save julienba/10681841 to your computer and use it in GitHub Desktop.

This post inspired me : http://www.krisbuytaert.be/blog/docker-vs-reality-0-1 !

When people say real world, they actually mean the sick, bitter and desperate world they imagined for you.

So how to produce "ahoy:234" with "ahoy:" and [1,2,3] ??

The idea [1] is "a program is composed of severals one-liners", lets try to port this one liner into several popular languages. :)

;; Install + launch gershwin
#_ "
cd `mktemp -d -t test`
wget http://clojars.org/repo/org/gershwinlang/gershwin/0.2.0/gershwin-0.2.0.jar
java -cp gershwin-0.2.0.jar gershwin.main
"

"ahoy:"
[1 2 3]

; with the stack, this is my preferred implementation, pure like a zen koan.
#[inc] map swap #[str2] reduce-with


; will use variables this time
(def prefix "ahoy:")
(def elements [1 2 3])


clear elements #[1 +] map prefix #[str2] reduce-with

;; I could use commas to tell you the size of the stack

clear elements , #[1 +] ,, map , prefix ,, #[str2] ,,, reduce-with ,  

;; with some string magic 
(use 'clojure.string)

clear 

"ahoy:" [1 2 3] ; put some data in the stack
#[inc] map join apply str2 ; do some string manipulation

 
; you can continue in the same gershwin repl session with some 'regular' Clojure


(reduce str prefix (map (partial + 1) elements))
(apply str prefix (map inc elements))

;; I totaly agree with you, this last part is a boring / tail windy.
;; Let's try some real syntax !
-- brew install ghc
-- ghci


let elements = [1,2,3]
let prefix="ahoy:"

-- There is maybe a better / point free way to do that !
-- As always it's pure, like algebra ! 
foldl (++) prefix (map (show . (+ 1)) elements)
prefix ++ (concatMap (show . (+ 1)) elements)


-- And There is a point free way, thanks to https://twitter.com/yoeight
import Control.Arrow
import Data.Char

let f = curry (uncurry (++) . second (fmap (intToDigit . succ . fromIntegral)))

f prefix elements

/*
Install + run scala

brew install scala
scala

or 

cd `mktemp -d -t test`
wget http://www.scala-lang.org/files/archive/scala-2.10.3.tgz
tar -xvf scala-2.10.3.tgz
./bin/scala

*/

val elements=List(1,2,3)
val prefix="ahoy:"

elements.map(x => (x + 1).toString).fold(prefix)(_+_) 
elements.map(_ + 1).mkString(start=prefix,sep="",end="")
prefix + elements.map(_ + 1).mkString

// this one is just to feed the troll
elements.map(_+1)./:(prefix)(_+_)
% brew install erlang
% erl

Elements=[1,2,3].
Prefix="ahoy:".

% Erlang is not known for -Number Crunching- String processing (also).
lists:foldl(fun(X,S) -> string:concat(S,X) end, Prefix,lists:map (fun(X) -> integer_to_list(X + 1) end, Elements)).
lists:concat([Prefix|(lists:map (fun(X) -> X+1 end,Elements))]) end.
% brew install elixir
% iex

elements=[1,2,3]
prefix="ahoy:"

% Elixir is great http://joearms.github.io/2013/05/31/a-week-with-elixir.html 
% and helps a bit with lambdas :)  
List.foldl(Enum.map(elements, &(integer_to_binary(&1 + 1))),prefix,&(&2 <> &1))
prefix <> Enum.map_join elements, &(&1 + 1)

% step by step
Enum.map(elements, &(&1 + 1))
|> Enum.map(&(to_string(&1)))
|>  Enum.reduce(prefix, &(&2 <> &1))

% or more compact
Enum.map(elements, &(to_string(&1 + 1))) |> Enum.reduce(prefix, &(&2 <> &1))

% without capture operator
Enum.map(elements, fn(x) -> to_string(x + 1) end) |> Enum.reduce(prefix, fn(x,acc) -> acc <> x end)
/*
brew install node
node
*/

var elements=[1,2,3];
var prefix="ahoy:";

elements.map(function(a){return (a + 1).toString();}).reverse().reduceRight(function(a,b){return a + b;}, prefix);
prefix + elements.map(function(a){return a+1;}).join("");

// thanks to https://twitter.com/blustemy
var acc=prefix;
elements.forEach(function(a) {acc = acc + (a + 1);});
acc;
  • [1] : clearly the worst idea I had lately, next time I will do nano-benchmarks with fibonacci series !
@ahoy-jon
Copy link

L'opérateur |> a l'air vraiment nice !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment