Last active
February 17, 2018 11:10
-
-
Save sporto/1b7943d8277ac5387fb95d8ae9ea757b to your computer and use it in GitHub Desktop.
Pipelines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html exposing (text) | |
multBy x = | |
(*) x | |
biggerThan x = | |
(<) x | |
nums = | |
[1,2,3] | |
result = | |
nums | |
|> List.map (multBy 2) | |
|> List.filter (biggerThan 4) | |
main = | |
text (toString result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply :: a -> (a -> b) -> b | |
apply x f = f x | |
infixl 0 |> | |
(|>) :: a -> (a -> b) -> b | |
x |> f = apply x f | |
multBy x = | |
(*) x | |
biggerThan x = | |
(<) x | |
nums :: [Int] | |
nums = | |
[1,2,3] | |
result :: [Int] | |
result = | |
nums | |
|> map (multBy 2) | |
|> filter (biggerThan 4) | |
main :: IO () | |
main = do | |
print result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nums = [1,2,3]; | |
function multBy(x, y) { | |
return x * y; | |
} | |
function biggerThan(x, y) { | |
return y > x; | |
} | |
const result = _(nums) | |
.map(_.curry(multBy)(2)) | |
.filter(_.curry(biggerThan)(4)) | |
.value() | |
console.log(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This doesn't work yet | |
infix operator |> { precedence 50 associativity left } | |
public func |> <T,U>(lhs: T, rhs: T -> U) -> U { | |
return rhs(lhs) | |
} | |
// func curry<A, B, C>(f: (A, B) -> C) -> A -> B -> C { | |
// return { a in { b in f(a, b) } } | |
// } | |
/// I.e. `flip(f)(x, y)` is equivalent to `f(y, x)`. | |
// public func flip<T, U, V>(f: (T, U) -> V) -> (U, T) -> V { | |
// return { f($1, $0) } | |
// } | |
// public func fmap(f: (A) -> B)(col: [<A>]) -> [<B>] { | |
// return col.map($0) | |
// } | |
public func fmap(f: (A) -> B) -> ([<A>] -> [<B>]) { | |
func map(col: [<A>]) -> [<B>] { | |
return col.map(f) | |
} | |
return map | |
} | |
/////////////////// | |
let numbers = [1,2,3] | |
func multBy(x: Int)(y: Int) -> Int { | |
return x * y; | |
} | |
func biggerThan(x: Int)(y: Int) -> Bool { | |
return y > x; | |
} | |
let result = numbers | |
|> fmap(multBy(4)) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment