Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elixir vs Ruby vs JavaScript Syntax

Elixir vs Ruby vs JavaScript Syntax

Compare Elixir syntax with Ruby and JavaScript #elixirschool

Maksym Verbovyi

August 23, 2020
Tweet

More Decks by Maksym Verbovyi

Other Decks in Programming

Transcript

  1. String "String" 'Not string' # [110, 111, 116, 32, 115,

    116, 114, 105, 110, 103] "String" 'string' "String" 'string'
  2. Map { name: "Maks", age: 32, height: 178 } {

    name: "Maks", age: 32, height: 178 }
  3. Map %{ name: "Maks", age: 32, height: 178 } {

    name: "Maks", age: 32, height: 178 } { name: "Maks", age: 32, height: 178 }
  4. Concatenation List [1,2,3] + [4,5] # [1,2,3,4,5] [1,2] << 3

    # [1,2,3] [1,2,3].concat([4,5]) // [1,2,3,4,5]
  5. Concatenation List [1,2,3] + [4,5] # [1,2,3,4,5] [1,2] << 3

    # [1,2,3] [1,2,3].concat([4,5]) // [1,2,3,4,5] [1,2,3] ++ [4,5] # [1,2,3,4,5] [3 | [1,2]] # [3,1,2]
  6. If statement if true # unless puts "It's true" else

    puts "It's false" end true ? "it's true" : "it's false"
  7. If statement if (true) { console.log("It's true") } else {

    console.log("It's false") } true ? "it's true" : "it's false"
  8. If statement if true do # unless IO.puts "It's true"

    else IO.puts "It's false" end if true, do: "It's true", else: "it's false"
  9. Case switch(true) { case true: "It's true" break; case false:

    "It's false" break; default: "Something else" }
  10. Case case true do true -> "It’s true" false ->

    "It’s false" _ -> "Something else" end
  11. Pipe operator [1, 2, 3, 4, 6]. map{ |x| x*x

    }. reduce{ |sum, x| sum + x }
  12. Pipe operator [1, 2, 3, 4, 6] .map(x => x

    * x) .reduce((x, sum) => sum + x) [1, 2, 3, 4, 6]. map{ |x| x*x }. reduce{ |sum, x| sum + x }
  13. Pipe operator [1, 2, 3, 4, 6] .map(x => x

    * x) .reduce((x, sum) => sum + x) [1, 2, 3, 4, 6] |> Enum.map(fn x -> x*x end) |> Enum.reduce(fn x, sum -> sum + x end) [1, 2, 3, 4, 6]. map{ |x| x*x }. reduce{ |sum, x| sum + x }
  14. Comprehensions for x in [1,2,3] do puts x end for

    (x in [1,2,3]) { console.log(x) }
  15. Comprehensions for x <- [1,2,3] do IO.puts x end for

    x in [1,2,3] do puts x end for (x in [1,2,3]) { console.log(x) }
  16. Anonymous function irb(main):010:0> hello = -> what { puts "Hello

    #{what}" } => #<Proc:0x00007fb13f0e8da8@(irb):10 (lambda)> irb(main):011:0> hello.("what") # hello.call("what") "Hello what" => nil
  17. Anonymous function irb(main):010:0> hello = -> what { puts "Hello

    #{what}" } => #<Proc:0x00007fb13f0e8da8@(irb):10 (lambda)> irb(main):011:0> hello.("what") # hello.call("what") "Hello what" => nil > const hello = (what) => { return `Hello ${what}`; } undefined > hello("world") 'Hello world'
  18. Anonymous function iex(10)> hello = fn what -> IO.puts "Hello

    #{what}" end #Function<7.126501267/1 in :erl_eval.expr/5> iex(11)> hello.("world") Hello world :ok irb(main):010:0> hello = -> what { puts "Hello #{what}" } => #<Proc:0x00007fb13f0e8da8@(irb):10 (lambda)> irb(main):011:0> hello.("what") # hello.call("what") "Hello what" => nil > const hello = (what) => { return `Hello ${what}`; } undefined > hello("world") 'Hello world'
  19. Companies Use the Elixir • Discord • Sketch • Pinterest

    • Bleacher Report • PepsiCo • Whatsapp • Financial Times