Skip to content

Instantly share code, notes, and snippets.

View lemastero's full-sized avatar
🕺
Everything is possible!

Piotr Paradziński lemastero

🕺
Everything is possible!
View GitHub Profile
@lemastero
lemastero / MonoidalCategories.scala
Created November 10, 2018 20:46
MonoidalCategory where underlying category is Category of Scala types and pure functions
/**
* Edward Kmett Discrimination is Wrong: Improving Productivity
* http://yowconference.com.au/slides/yowlambdajam2015/Kmett-DiscriminationIsWrong.pdf
*/
object MonoidalCategoriesForCategoryOfScalaTypes {
import scala.language.higherKinds
trait Bifunctor[P[_,_]] {
def bimap[A, B, C, D](f: A => B, g: C => D): P[A, C] => P[B, D]
}
@lemastero
lemastero / ProfunctorOptics.scala
Last active August 28, 2020 00:02
Profunctor Optics in Scala
import scala.language.higherKinds
/*
Mainline Profunctor Heirarchy for Optics: https://r6research.livejournal.com/27476.html
Profunctor Optics: The Categorical Approach - Bartosz Milewski: https://www.youtube.com/watch?v=l1FCXUi6Vlw
*/
object ProfunctorOpticsSimpleImpl {
trait Functor[F[_]] {
def map[A, B](x: F[A])(f: A => B): F[B]
@lemastero
lemastero / gist:a282d75a700d5a12b13b63ec917b6b3d
Created November 15, 2018 22:21 — forked from runarorama/gist:a8fab38e473fafa0921d
Compositional application architecture with reasonably priced monads
sealed trait Interact[A]
case class Ask(prompt: String)
extends Interact[String]
case class Tell(msg: String)
extends Interact[Unit]
trait Monad[M[_]] {
def pure[A](a: A): M[A]
@lemastero
lemastero / gist:b8ad730644f453045b4056eb670f4dfb
Created November 15, 2018 22:25 — forked from runarorama/gist:33986541f0f1ddf4a3c7
Higher-kinded types encoded as path-dependent types
trait λ {
type α
}
trait Functor extends λ {
type α <: λ
def map[A,B](x: α { type α = A })(f: A => B): α { type α = B }
}
@lemastero
lemastero / Adjunctions.scala
Created November 15, 2018 22:27 — forked from runarorama/Adjunctions.scala
Free/forgetful adjunctions
import scalaz._, Scalaz._
// Adjunction between `F` and `G` means there is an
// isomorphism between `A => G[B]` and `F[A] => B`.
trait Adjunction[F[_],G[_]] {
def leftAdjunct[A, B](a: A)(f: F[A] => B): G[B]
def rightAdjunct[A, B](a: F[A])(f: A => G[B]): B
}
// Adjunction between free and forgetful functor.
@lemastero
lemastero / kp-monoid.scala
Created November 22, 2018 11:39 — forked from mandubian/kp-monoid.scala
"Monad is a monoid in the category of endofunctors" evidence using trivial sample of Scala Kind-Polymorphism PoC
/** Showing with Kind-Polymorphism the evidence that Monad is a monoid in the category of endofunctors */
object Test extends App {
/** Monoid (https://en.wikipedia.org/wiki/Monoid_(category_theory))
* In category theory, a monoid (or monoid object) (M, μ, η) in a monoidal category (C, ⊗, I)
* is an object M together with two morphisms
*
* μ: M ⊗ M → M called multiplication,
* η: I → M called unit
*
@lemastero
lemastero / ProfunctorAndStrong.hs
Created November 25, 2018 14:35
Simple Haskell implementation for Profunctor and Strong Profunctor to analyse Strong Profunctor laws
swap (a, b) = (b, a)
class Profunctor p where
dimap :: (aa -> a) -> (b -> bb) -> p a b -> p aa bb
lmap :: (aa -> a) -> p a b -> p aa b
lmap f = dimap f id
rmap :: (b -> bb) -> p a b -> p a bb
rmap = dimap id
@lemastero
lemastero / Sudoku.hs
Created January 3, 2019 13:05 — forked from danoneata/Sudoku.hs
Applicative-based Sudoku solver
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Control.Applicative
import Data.Char
import Data.List (intersperse)
import Data.Monoid hiding (All, Any)
import Data.Foldable hiding (all, any)
import Prelude hiding (all, any)
@lemastero
lemastero / ttg.hs
Created January 28, 2019 09:30 — forked from tonymorris/ttg.hs
Trees That Grow
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE StandaloneDeriving #-}
@lemastero
lemastero / WordsAboutNothingAndAny.scala
Last active January 28, 2019 23:47
What we say when we say F.. Yea! or F... No!
import scalaz.{Comonad, Monad}
import scalaz.{Arrow, Contravariant, Profunctor}
import scalaz.{Lan, Ran}
object AnyAndNothingInstances {
// Monad
// https://twitter.com/runarorama/status/1085383309969014784
type FYea[A] = Any