Skip to content

Instantly share code, notes, and snippets.

View Softsapiens's full-sized avatar

Daniel Pous Montardit Softsapiens

View GitHub Profile
@Softsapiens
Softsapiens / WithGlobals.scala
Created May 2, 2017 11:05 — forked from sjrd/WithGlobals.scala
I wrote a monad
/* __ *\
** ________ ___ / / ___ __ ____ Scala.js tools **
** / __/ __// _ | / / / _ | __ / // __/ (c) 2013-2017, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ **
** /____/\___/_/ |_/____/_/ | |__/ /____/ **
** |/____/ **
\* */
package org.scalajs.core.tools.linker.backend.emitter
@Softsapiens
Softsapiens / Fib.scala
Created March 9, 2017 16:44 — forked from dscleaver/Fib.scala
Port of http://t.co/KvoY7PDGSg. Created in a Scala IDE worksheet.
import scalaz._
import Scalaz._
object monads {
def fix[A](f: (=> A) => A): A = f(fix(f)) //> fix: [A](f: => A => A)A
type Gen[A] = (=> A) => A
def gFib: Gen[Int => Int] = (self) => n =>
@Softsapiens
Softsapiens / ASample.scala
Created January 20, 2017 17:30 — forked from mandubian/ASample.scala
Encoding Category Theory Laws (à-la-scalaz/cats) using kind-polymorphic List and compare/manipulate laws of structure
/**
* In classic Scala implementation of hierarchies of Category theory laws (scalaz/cats), the dependency between
* different laws is encoded using inheritance (https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/Monad.scala#L14)
*
* This approach creates well-known issues: for example, a Monad is able to _derive_ an Applicative.
* So sometimes, you don't have the Applicative you want but the one derived from the Monad or it doesn't
* compile because you have 2 Applicatives in your implicit scope.
*
* Alois Cochard has proposed a new model to represent that in (scato project)[https://github.com/aloiscochard/scato] or
* (scalaz/8.0.x)[https://github.com/scalaz/scalaz/blob/series/8.0.x/base/src/main/scala/typeclass/Monad.scala] branch.
@Softsapiens
Softsapiens / any-order-ubiklist.scala
Created December 2, 2016 09:18 — forked from mandubian/any-order-ubiklist.scala
UbikList or abstracting heterogenous list to anykind
// Basic shapeless-style HList
sealed trait HList
sealed trait HNil extends HList {
def ::[H](h : H) = Test.::(h, this)
}
final case object HNil extends HNil
final case class ::[+H, +T <: HList](head : H, tail : T) extends HList
@Softsapiens
Softsapiens / minimal-http.hs
Created December 2, 2016 09:18 — forked from aaronlevin/minimal-http.hs
Minimal Haskell HTTP server written on top of warp via the Web Application Interface (no frameworks)
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Network.Wai (pathInfo, Request, requestMethod, Response, responseLBS, ResponseReceived)
import Network.Wai.Handler.Warp (run)
import Network.HTTP.Types (status200, status401)
-- note: type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
application :: Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
@Softsapiens
Softsapiens / pp-comonad.hs
Created November 26, 2016 13:50 — forked from jtobin/pp-comonad.hs
Probabilistic programming using comonads.
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
import Control.Comonad
import Control.Comonad.Cofree
import Control.Monad
import Control.Monad.ST
@Softsapiens
Softsapiens / Counter.purs
Created November 26, 2016 13:14 — forked from Dierk/Counter.purs
A graphical multiplication table written in PureScript/Pux, try at https://dierk.github.io/tryPux/beautifulMath/index.html
module App.Counter where
import Prelude (($), (+), (-), (*), (/), (>), const, show)
import Data.Array ((..), (:), mapWithIndex)
import Data.Int (toNumber, floor)
import Math (sin, cos, pi )
import Pux.Html (Html, div, span, button, text, canvas, svg, circle, line )
import Pux.Html.Attributes (width, height, viewBox, cx, cy, r, fill, x1, y1, x2, y2, stroke, strokeWidth)
import Pux.Html.Events (onClick)

Rust/Haskell: Higher-Kinded Types (HKT)

A higher kinded type is a concept that reifies a type constructor as an actual type.

A type constructor can be thought of in these analogies:

  • like a function in the type universe
  • as a type with a "hole" in it
{-# LANGUAGE InstanceSigs #-}
module Chapter26 where
import Control.Monad (liftM)
newtype MaybeT m a = MaybeT' { runMaybeT :: m (Maybe a) }
instance (Functor m) => Functor (MaybeT m) where
fmap f (MaybeT' ma) = MaybeT' $ (fmap . fmap) f ma