Skip to content

Instantly share code, notes, and snippets.

@firekesti
Created January 30, 2018 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firekesti/9e4e1f551026d1735a3e5196037732de to your computer and use it in GitHub Desktop.
Save firekesti/9e4e1f551026d1735a3e5196037732de to your computer and use it in GitHub Desktop.
Less Imperative with More Kotlin
Slides:
https://speakerdeck.com/queencodemonkey/less-imperative-with-more-kotlin
Getting into RxJava, someone said "it's just like a monad" - and, what??
func programming: can be high-level, abstract, etc
imperative: a sequence of statements that change state.
actions, specific control flow. *how* to do something.
declarative: *what* you want to do (maybe a little fluffy, but _chill_)
HTML: there's no _how_! just a description of structure.
SQL: describe what you want, receive it.
imperative: talking to a toddler
declarative: talking to a responsible adult
functional: more of a _style_. disagreement on exact definition.
based on evaluating expressions/computations
monads are like burritos???
side effect ~ when a function makes modifications of outside state.
side cause ~ hidden input, from outside state
more member variables = more difficult to understand/test.
would have to recreate environment/mocks. source of bugs.
func code is characterized by one thing: ~*absence of side effects*~
"Pure Fucntion": idempotent, output depends only on inputs.
"nullimpotent": calling it zero times = calling it once.
"referential transparency": a func can be replaced by its evaulated value.
basically, no side effects allowed. encourages separation of concerns.
very easy to understand *and test*! safer mulithreading.
how Huyen got into Kotlin: enchanted with syntax, then other features, then functional programming
higer-order functions: take functions as inputs, might even return a function!
function types, lambdas. stdlib has func APIs/operators.
Reference the slides for this part:
MAPPING:
Imperative: given list of Movie, create new array, foreach get name, add to array, return array of names.
Functional: given list of Movie, return movies.map(movie -> movie.name)
fun Iterable<T>.map(transform:(T)->R): List<R>
fun getFilmTitles(films : List<Movie>) = films.map{it.name}
or, property reference: films.map(Movie::name)
FOLDING:
films.fold(0){gross, film -> gross + film.gross}
fun Iterable<T>.fold(initial:R, operation:(acc:R,T)->R):R
FILTERING:
films.filter{it.year == 2017}
fun Iterable<T>.filter(predicate:(T)):boolean
passing these around:
val greaterThan100m = { movie: Movie -> movie.gross > 100 }
println ("$films.filter(greaterThan100m)}") :o
Takeaways:
strongest I've felt about filter/fold/map
rx/functional paradigms clicked a lot more
kotlin's syntax looks fun and concise!
really lends itself to functional style
side effects are terrible!!! ahh!!!!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment