Skip to content

Instantly share code, notes, and snippets.

@LukaJCB
LukaJCB / TypeClassesDataTypes.purs
Created February 9, 2017 08:59
Why Typeclass constraints on Data types would be great.
type Container f1 f2 f3 =
{ children :: f1 String
, friends :: f2 Int
, colleagues :: f3 Number
}
data SomeType f1 f2 f3 a e = Container (Container f1 f2 f3)
| AnotherType a
| SomeEffect (Eff e Unit)
import Html exposing (beginnerProgram, div, button, text, ul, li, input, Html)
import Html.Events exposing (onClick, onInput)
import Array exposing (Array, toList, fromList, push, filter)
main =
beginnerProgram { model = initialState, view = view, update = update }
type alias Model = { list: Array String, currentText: String }
val nameStream = createInputHandler()
val component = div(
label(className := "label", "Name:"),
input(inputType := "text", input --> nameStream),
hr(),
h1("Hello ", child <-- nameStream)
)
render(document.getElementById("app"), component)
//So now after letting IntelliJ convert our file to Kotlin we get this:
//... but there's an error
class ExampleActivity : AppCompatActivity() {
private var foo: String? = "Bar"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
@LukaJCB
LukaJCB / NullableImmutable.java
Created September 2, 2016 16:44
Exemplifies the lack of safety when using Java
class ExampleActivity extends AppCompatActivity {
private String foo = "Bar";
@Override
public void onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
foo = getSomething();
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
val res = arr match {
case Array(0) => "0"
case Array(x, y) => x + " > " + y
case Array(0, _*) => "0 > ..."
case _ => "..."
}
val list = Stream(1,2,3,4,5,6,7,8,9)
val list2 = list.map(_ + 8)
.filter(_ % 2 == 0)
.map(_ * 4)
println(list2.head)
var arr = [1,2,3,4,5,6,7,8,9]
var arr2 = arr.map(x => x + 8)
.filter(x => x % 2 == 0)
.map(x => x * 4)
console.log(arr2[0])
add :: Integer -> Integer -> Integer
add a b = a + b