Herramientas de usuario

Herramientas del sitio


wiki2:elm-code

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
wiki2:elm-code [2020/12/25 13:29]
alfred [Gotchas]
wiki2:elm-code [2020/12/26 08:03] (actual)
Línea 15: Línea 15:
 priceInEuros 3.3  -- 3.63 priceInEuros 3.3  -- 3.63
 </​code>​ </​code>​
-==== Function composition (?) ====+==== Partially applied functions ​====
  
 Every function that accepts two arguments can be converted in one that accepts one argument: Every function that accepts two arguments can be converted in one that accepts one argument:
Línea 26: Línea 26:
 </​code>​ </​code>​
  
 +Then there is this:
 +<​code>​
 +double = (*) 2
 +<​function>​ : number -> number
 +</​code>​
  
 +This can happen because all functions accept only and exactly one argument.
  
-==== Simplify types ====+<​code>​ 
 +> greeting : String -> String -> String 
 +| greeting greet name greet ++ ", " ++ name ++ "​!"​ 
 +<​function>​ : String -> String -> String 
 +> greeting "​Hello"​ "​DailyDrip" ​== ((greeting "​Hello"​) "​DailyDrip"​) 
 +True : Bool 
 +</​code>​
  
-type alias MyType = Int -> String -> String +Parentheses are optional because function evaluation associates to the left by default. 
-mytrial: MyType + 
-mytrial n s = String.repeat n s +<​code>​ 
-mytrial 3 "​abc"​+> (*) 
 +<​function>​ : number -> number -> number 
 +double = (*) 2 
 +<​function>​ : number -> number 
 +</​code>​ 
 +As we can see we can think that (*) takes two numbers as arguments. On double function the compiler will really infer that our double function takes one number as its sole argument. 
 + 
 +Then we can easily create a function for doubling values on a list: 
 +<​code>​ 
 +doubleList = List.map double 
 +-- <​function>​ : List number -> List number 
 +</​code>​ 
 + 
 +This is the main reason why [[https://​package.elm-lang.org/​help/​design-guidelines#​the-data-structure-is-always-the-last-argument|the data structure should be always the last argument]]. 
 + 
 +He have also this: 
 +<​code>​ 
 +> (|>) 
 +<​function>​ : a -> (a -> b) -> b 
 + 
 +amountDeposited : List Transaction -> Float 
 +amountDeposited list = 
 +    List.filter (\t -> t.type_ == Deposit) list 
 +        |> List.map .amount 
 +        |> List.sum 
 + 
 +-- It filters all the transactions in list which are deposits and obtain the amount for, at the last step, sum them. 
 +-- rather than the nested equivalent:​ 
 +-- List.sum (List.map .amount (List.filter (\t -> t.type_ == Deposit) list)) 
 +</​code>​ 
 +==== Simplify types ==== 
 +<​code>​ 
 +type alias MyType = Int -> String -> String 
 +mytrial: MyType 
 +mytrial n s = String.repeat n s 
 +<​function>​ : Int -> String -> String 
 +mytrial 3 "​abc"​ 
 +"​abcabcabc"​ : String 
 +</​code>​
wiki2/elm-code.1608902975.txt.gz · Última modificación: 2020/12/25 14:29 (editor externo)