Skip to content

Instantly share code, notes, and snippets.

@dcastro
dcastro / Affjax in psci
Last active January 27, 2017 11:02
Using purescript-affjax in psci
-- npm i xhr2 --save
import Prelude
import Control.Monad.Aff
import Control.Monad.Eff.Console
import Network.HTTP.Affjax
void $ runAff logShow (\x -> log x.response) (get "https://httpbin.org/ip" :: Affjax (console :: CONSOLE) String)
void $ runAff logShow (\x -> log (x.response :: String)) (get "https://httpbin.org/ip")
@dcastro
dcastro / option.purs
Created June 23, 2017 07:58
Church-encoding of `Option`/Maybe` in Purescript
module Main where
import Prelude
import Control.Monad.Eff.Console
import Data.Foldable (foldMap)
import TryPureScript
type Option a = forall b. (a -> b) -> b -> b
@dcastro
dcastro / angular.testdoubles.js
Last active August 9, 2017 08:36
AngularJS - test doubles, cheat sheet
// Testing a service with no dependencies
it('description', inject(function(myService) {
expect(myService).to.work.dammit;
}));
// Setting up some test doubles (`dependency` and `dependency2`)
it('description2', function() {
module({
dependency: {
property: 'hello',
@dcastro
dcastro / nested-nullables.kt
Last active September 4, 2017 18:41
Nested nullables in Kotlin
// A function that takes an instance of `T` and returns a nullable `T?`
fun <T>toNullable(t: T): T? {
return t
}
// If we pass in a `String`, we get back a `String?`
val s1 : String? = toNullable("" as String)
// So, if we pass in a `String?`, we get back a nested nullable `String??`, right?
// Wrong. We get a `String?`
@dcastro
dcastro / check-markdown-links.js
Created September 8, 2017 10:10 — forked from estliberitas/check-markdown-links.js
Script checking that all links with references are present in Markdown file.
{-# LANGUAGE TypeFamilies, FlexibleContexts #-}
type Position = (Int, Int)
newtype PlayerData = PlayerData { _pos :: Position }
data GameStateData = GameStateData PlayerData [Position]
-- player typeclass and instance
class Player p where
@dcastro
dcastro / expanding.hs
Created September 27, 2017 11:57
Expanding `>>=` on functions
-- start with
(+) >>= id $ 2
-- Expand `>>=` using the Monad instance for functions
-- instance Monad ((->) r) where
-- f >>= k = \ r -> k (f r) r
(\r -> id ((+) r) r) $ 2
-- beta reduction
id ((+) 2) 2
@dcastro
dcastro / defining-properties.sc
Last active November 28, 2017 21:54
Scalacheck demo
/*
* useful links
*
* https://github.com/rickynils/scalacheck/blob/master/doc/UserGuide.md
* http://www.scalatest.org/user_guide/generator_driven_property_checks
*
*/
import org.scalacheck._
import org.scalacheck.Arbitrary._
import monix.execution.Scheduler.Implicits.global
import monix.eval._
import io.circe._
import io.circe.generic.semiauto._
import de.heikoseeberger.akkahttpcirce.ErrorAccumulatingCirceSupport._
/**
POST localhost:9999/mock
{
@dcastro
dcastro / circe_shapeless_record.scala
Created April 27, 2018 14:32
Circe + Shapeless: blacklist fields with type-safety
import shapeless._, record._
import io.circe._
import io.circe.syntax._
import io.circe.generic.encoding._
case class Person(name: String, age: Int)
object Person {
implicit val encodePerson: Encoder[Person] =
ReprObjectEncoder.deriveReprObjectEncoder.contramap { person =>