Skip to content

Instantly share code, notes, and snippets.

@surajsau
Last active March 13, 2020 13:58
Show Gist options
  • Save surajsau/135b163514ca911a330fa8a166c7da84 to your computer and use it in GitHub Desktop.
Save surajsau/135b163514ca911a330fa8a166c7da84 to your computer and use it in GitHub Desktop.
typealias StringPair = Pair<String, String>
typealias IntPair = Pair<Int, Int>
//this is prone to exception if str isn't in the right format e.g., "1, 2, 3, 4, 5"
val split: (String) -> Pair<StringPair, StringPair> = { str ->
val (a, b, c, d) = str.split(",")
Pair(Pair(a, b), Pair(c, d))
}
val convertToInt: (pair: Pair<StringPair, StringPair>) -> Pair<IntPair, IntPair> = { pair ->
Pair(Pair(pair.first.first.toInt(), pair.first.second.toInt()),
Pair(pair.second.first.toInt(), pair.second.second.toInt()))
}
val transform: (pair: Pair<IntPair, IntPair>) -> Int = { pair ->
(pair.first.first + pair.first.second)/(pair.second.first + pair.second.second)
}
fun <A,B,C>combine(f: (B) -> C, g: (A) ->B): (A) -> C {
return { x -> f(g(x)) }
}
val result = combine(combine(transform, convertToInt), split)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment