Friday, May 13, 2016

Elm basic snippet

Elm

If

If and else are something different here without curly brackets {}
if "10" == "10" then "Its ten" else "Not ten"
If Not equal has different symbol here,
if "10" /= "11" then "Its ten" else "Not ten"

String Util

 - toInt
If you want to parse String to Int , this is the right util. The below method will return True if the String is parsable to Int or else otherwise Generaly String.toInt will return Result String Int , Which holds the Value / Error. The below method (validateStringToInt) would be the sample to extract the result value
String.toInt "10"`
Ok 10 : Result.Result String Int
validateStringToInt x =   
  case (String.toInt x) of
    Ok value -> True
    Err msg -> False

String Contact

Use ++ instead of regular + operator to concat. Every thing value here (Immutable) when doing contact it will give your new value
a = "one " ++ "two"
"one two" : String
a ++ "end"
"one twoend" : String

Tuple

- Tuple
Tuples are simple structure which hold the data in a order to server.
(10,11)
Access Tuple
fist element of the typle
fst (10,11)
second element of the tuple
snd (10,11)

Lambda expression

Forward slash for the parameter and -> function body
(\x -> x * 2)
Below code is for maping list to double list
double list = List.map (\x -> x * 2) list
<function> : List number -> List number
double [2,3,4]
[4,6,8] : List number
In Short lambda can be written like for single element , that automaticaly taken as default parameter
double list = List.map ((*) 2) list

No comments:

Post a Comment

Merging two sorted arrays - Big O (n+m) time complexity

 Problem :  Merge the two sorted arrays. Edge case :  Array can empty Arrays can be in different size let getMaxLength = ( input1 , input...