Learn You a erlang for Great Good!
1) Introduction
I'm currently learning scala and playing with scala actors. Pretty much every blogs mention how erlang is great for building highly scalable systems. So, wanted to learn bit about erlang itself. Did not enter erlang actor model yet but want to start with the basic stuffs.
2) Starting out
2.1 - install erlang
2.2 - Tuples
tuple in any lang is a datastructure to store a sequence of data which could be multiple types.
2.3 - List datastructure
1) Introduction
I'm currently learning scala and playing with scala actors. Pretty much every blogs mention how erlang is great for building highly scalable systems. So, wanted to learn bit about erlang itself. Did not enter erlang actor model yet but want to start with the basic stuffs.
2) Starting out
2.1 - install erlang
brew install erlang
tuple in any lang is a datastructure to store a sequence of data which could be multiple types.
27> ShirtInventory = {"shirts", 100, 88}.
{"shirts",100,88}
Pattern match on tuples,
28> {Item, Qty, UnitPrice} = ShirtInventory.
{"shirts",100,88}
29> Item.
"shirts"
2.3 - List datastructure
a sequence of data of same/different types, unlike tuples can have unknown number of elements in it. has head and tail.
7> [1, 3, 5, 7] ++ [11, 13, 17, 19].
[1,3,5,7,11,13,17,19]
1> FibList = [1, 1, 2, 3, 5].
[1,1,2,3,5]
2> AnotherFibList = [0 | FibList].
[0,1,1,2,3,5]
#list pattern match
4> [Head|Tail] = AnotherFibList.
[0,1,1,2,3,5]
522> Head.
0
5> Tail.
[1,1,2,3,5]
6> [H | T] = Tail.
[1,1,2,3,5]
7> H.
1
8> T.
[1,2,3,5]
List comprehension - ways to build or modify lists.
[0.85*Price || Price <- [100,200,300,400]]. ## get discounted prices for items in cart 2> Cart = [{"nexus", 100},{"woofer", 200},{"mic", 300},{"Drum", 400}]. [{"nexus",100},{"woofer",200},{"mic",300},{"Drum",400}] 3> [0.85*Price || {Item, Price} <- Cart]. [85.0,170.0,255.0,340.0]
No comments:
Post a Comment