Shih • B.S., M.S. of NTU CSIE • Worked in WOOMOO • Worked in Mobiusbobs • Freelancer for now, next stop: FB in London • A Fullstack Software Engineer, including • iOS, Android, Web backend/Frontend • Learn Haskell/PureScript as a hobby :D
React solve them? ▸ Event handling ▸ View update ▸ Async operations and other side effects ▸ The problems we still have after React. ▸ What does PureScript help? ▸ Some examples of PureScript
them? ▸ Event handling ▸ View update ▸ Async operations and other side effects ▸ The problems we still have after React. ▸ What does PureScript help? ▸ Some examples of PureScript
button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Code here executes on main thread // after user presses button } }); $("button").on('click', () => { display.text(parseInt(display.text()) + 1) }) No first class function So need a anonymous object First class function: Simple! from https://developer.android.com/reference/android/widget/Button.html
CLASS Function<Integer,Integer> add1 = x -> x + 1; Integer two = add1.apply(1); // yields 2 const add1 = x => x + 1 const two = add1(1) First class function: Simple! This is how to invoke a anonymous function in Java 8 https://dzone.com/articles/functional-programming-java-8
= (f, user) => { const { loginCount } = user return { ...user, loginCount: f(loginCount) } } const add = (a, b) => a + b updateCounter(add(1), user) // nope you can't There are some library can do auto curry, but often make the type checker fails.
them? ▸ Event handling => First class function ▸ View update ▸ Async operations and other side effects ▸ The problems we still have after React. ▸ What does PureScript help? ▸ Some examples of PureScript
them? ▸ Event handling => First class function ▸ View update ▸ Async operations and other side effects ▸ The problems we still have after React. ▸ What does PureScript help? ▸ Some examples of PureScript
FlowType ▸ Ramda is kind of ok with FlowType but very fragile ▸ The problem is type system need to be strong and 100% enforced, otherwise you can’t trust it. ▸ Due to the same reason, FB is building ReasonML. Ref: https://youtu.be/tCVXp6gFD8o
solve them? ▸ Event handling => First class function ▸ View update => Pure & Static type checker ▸ Async operations and other side effects ▸ The problems we still have after React. ▸ What does PureScript help? ▸ Some examples of PureScript
solve them? ▸ Event handling => First class function ▸ View update => Pure & Static type checker ▸ Async operations and other side effects ▸ The problems we still have after React. ▸ What does PureScript help? ▸ Some examples of PureScript
= yield call(queryA) const b = yield call(queryB, a) const c = yield call(queryC, b) yield put({ type: 'UPDATE', a, b, c}) } This is what we want right? There is also async/await but we can actually do more How to combine those ??????
them? ▸ Event handling => First class function ▸ View update => Pure & Static type checker ▸ Async operations and other side effects => ”Special” function composition ▸ The problems we still have after React. ▸ What does PureScript help? ▸ Some examples of PureScript
WELL ▸ First class function => No built in curry ▸ Purity/Immutable => No way to guarantee ▸ Static type analysis => None of them are perfect ▸ “Special” function composition => generator kind of does this job, but we can do it better
them? ▸ Event handling => First class function ▸ View update => Pure & Static type checker ▸ Async operations and other side effects => ”Special” function composition ▸ The problems we still have after React. ▸ What does PureScript help? ▸ Some examples of PureScript
them? ▸ Event handling => First class function ▸ View update => Pure & Static type checker ▸ Async operations and other side effects => ”Special” function composition ▸ The problems we still have after React. ▸ What does PureScript help? ▸ Some examples of PureScript
-> Int -> Int add a b = a + b arr = [1, 2, 3] map (add 1) arr -- [2, 3, 4] In PureScript In JS const add = (a: number) => (b: number): number => a + b const arr = [1, 2, 3] arr.map(add(1))
ASYNC QUERY In PureScript In JS function* fetch() { const a = yield call(queryA) const b = yield call(queryB, a) const c = yield call(queryC, b) yield put({ type: 'UPDATE', a, b, c}) } fetch = do a <- queryA b <- queryB a c <- queryC b dispatch a b c
LIST In PureScript In JS const getGreatGrandChildren = person => getChildren(person) .map(getChildren).flatten() .map(getChildren).flatten() getGreatGrandChildren person = do child <- getChildren person grandChild <- getChildren child getChildren grandChild
redux ▸ e.g. https://github.com/rayshih/pux-todomvc ▸ Lens — cool library to operate on nested data and more ▸ Async — you need this to perform async tasks ▸ Simple JSON — the easiest way to parse JSON safely