Skip to content

Instantly share code, notes, and snippets.

View milankinen's full-sized avatar
🐦
tsers

Matti Lankinen milankinen

🐦
tsers
View GitHub Profile
@milankinen
milankinen / liftVDOM.js
Last active November 3, 2015 10:34
React VDOM(Bacon.Observable) => Bacon.Observable(VDOM)
import Bacon from "baconjs"
import React from "react"
import {cloneAndReplaceProps, isValidElement} from "react/lib/ReactElement"
import {find, isEmpty} from "lodash"
/**
* Transforms: VDOM(Observable) => Observable(VDOM)
* Example:
* const a = Bacon.constant(4)
* const b = Bacon.constant(3)
@milankinen
milankinen / application.js
Created October 23, 2015 10:25
Bottom-up-MÖLLYKKÄ
import React from "react"
import {combineAsArray} from "baconjs"
import Counter from "./components/counter"
import BigList from "./components/bigList"
export default initialState => (
combineAsArray(Counter(initialState), BigList()).map(
([$counter, $list]) => (
<div>
<h1>Tsers!</h1>
@milankinen
milankinen / index.js
Last active September 25, 2015 09:56
Bacon.js flatMapFirst
const Bacon = require("baconjs")
Bacon
.sequentially(10, [1, 2, 3, 4, 5])
.flatMapFirst(val => Bacon.later(25, val))
.subscribe(console.log.bind(console))
@milankinen
milankinen / flatUpdate.js
Last active February 8, 2017 00:43
Bacon flatUpdate
const Bacon = require("baconjs")
/**
* flatUpdate :: (state, [Observable+, [(state,...events)=>Observable<A>, (state, A)=>newState]]+) => Observable<state>
*
* const stateP = flatUpdate(initialState,
* [event1S], (state, newState) => newState, // supports normal Bacon.update
* [event2S], (state, newState) => Bacon.later(100, newState) // supports delayed state updating
* [event3S], [submitForm, handleSubmitResult] // supports 2-stage state updating
* )
@milankinen
milankinen / optimistic.js
Last active August 29, 2015 14:22
Bacon.js optimistic/pessimistic updates
module.exports = {
toProperty(initialItems) {
const updateItemS =
d.stream("item:update")
const updateResultS =
updateItemS
.flatMapLatest(data => Bacon.fromPromise(server.updateItem(data)))
.mapError({result: "failure", msg: "Server error"})
@milankinen
milankinen / example.js
Created May 28, 2015 20:02
react-router with livereactload 0.5.2
var React = require('react'),
Router = require('react-router'),
lrApi = require('livereactload-api')
var { Route, RouteHandler, Link } = Router
var App = React.createClass({
contextTypes: {
router: React.PropTypes.func
@milankinen
milankinen / dev
Last active August 29, 2015 14:18
react-router with LiveReactload
#!/bin/bash
node_modules/.bin/nodemon --ignore public/bundle.js &
{ { node_modules/.bin/watchify site.js -v -t babelify -g livereactload -o static/bundle.js 1>&2; } 2>&1 \
| while read result; do
echo "$result"
[[ "$result" =~ ^[0-9]+[[:space:]]bytes[[:space:]]written ]] && node_modules/.bin/livereactload notify
done
} &
@milankinen
milankinen / OrderingDSL.scala
Last active December 26, 2015 00:09
Scala ordering DSL
object OrderingDSL {
case class AscendingOrder[T](value: T)(implicit ord: Ordering[T]) extends Ordered[AscendingOrder[T]] {
def compare(that: AscendingOrder[T]): Int = ord.compare(value, that.value)
}
case class DescendingOrder[T](value: T)(implicit ord: Ordering[T]) extends Ordered[DescendingOrder[T]] {
def compare(that: DescendingOrder[T]): Int = -ord.compare(value, that.value)
}
def asc[T](value: T)(implicit ord: Ordering[T]) = AscendingOrder(value)
@milankinen
milankinen / OrderingDSLExample.scala
Last active December 26, 2015 00:09
Scala ordering DSL example
object OrderingDSLExample extends App {
case class My(num: Int, str: String)
val myValues = List(My(3, "foo"), My(3, "bar"), My(5, "bar"), My(5, "foo"))
import OrderingDSL._
require(myValues.sortBy(m => (asc(m.num), asc(m.str))) ==
List(My(3, "bar"), My(3, "foo"), My(5, "bar"), My(5, "foo")))
require(myValues.sortBy(m => (asc(m.num), desc(m.str))) ==
List(My(3, "foo"), My(3, "bar"), My(5, "foo"), My(5, "bar")))
<script type="text/x-template" id="person-form-template">
<div class="form-horizontal">
<div class="control-group">
<label class="control-label" for="name">Name</label>
<div class="controls"><input class="input" type="text" name="name" id="name" value="{{name}}" /></div>
</div>
<div class="control-group">
<label class="control-label" for="age">Age</label>
<div class="controls"><input class="input" type="number" name="age" id="age" value="{{age}}" /></div>
</div>