Skip to content

Instantly share code, notes, and snippets.

View tscholak's full-sized avatar
🚵‍♂️
(>>=) :: m a -> (a -> m b) -> m b

Torsten Scholak tscholak

🚵‍♂️
(>>=) :: m a -> (a -> m b) -> m b
View GitHub Profile
@tonymorris
tonymorris / free-classy-prisms.hs
Created April 28, 2017 07:35
Free monad with classy prisms on grammar
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveFunctor #-}
import Control.Lens
import Prelude hiding (readFile, writeFile, print)
import qualified Prelude as Prelude(readFile, writeFile, print)

Why not both?

With the recent announcement of cats-effect, a relevant question from the past resurfaces: why does IO, which is otherwise quite Task-like, not define both or race? To be clear, the type signatures of these functions would be as follows:

object IO {
  def both[A, B](ioa: IO[A], iob: IO[B])(implicit EC: ExecutionContext): IO[(A, B)] = ???
  def race[A, B](ioa: IO[A], iob: IO[B])(implicit EC: ExecutionContext): IO[Either[A, B]] = ???
}
@b-studios
b-studios / 01.README.md
Last active November 23, 2020 22:58
Syntactic sugar for step-by-step annotated functions in pointfree style

When defining complex functions in pointfree style, I often find myself switching to pointful style. Sometimes, I even convert to ANF and annotate the types to understand the steps.

After completing the function definition, I often convert back to pointfree style for conciseness. End of the story: To understand it again a couple of weeks later, I start expanding to annotated pointful again.

The small 10-lines library at the end of this post allows to define pointfree functions with intermediate type annotations.

Credits: Agda and EqReasoning for syntactical inspiration.

@mrkgnao
mrkgnao / Neural.idr
Last active July 30, 2022 15:00
A quick Idris implementation of @mstksg's "dependent Haskell" neural networks
module Main
import Data.Vect
-- %hide transpose
dot : Num a => Vect n a -> Vect n a -> a
dot va vb = foldr (+) 0 $ zipWith (*) va vb
Matrix : (rows : Nat) -> (cols : Nat) -> Type -> Type
@pchlupacek
pchlupacek / fs2Conversion.scala
Last active April 22, 2017 04:57
converting fs2 to scalaz.stream and vice versa
package spinoco.scalaz.stream
import fs2.Handle
import fs2._
import scalaz.concurrent.{Actor, Task}
import scalaz.stream.{Cause, Process, wye}
import scala.language.higherKinds
import scalaz.{-\/, \/, \/-}
/*
Results
[info] Running StreamsExperiments
[info] Count: 923062246
[info] Time: 5301
[info] Count: 923062246
[info] Time: 4588
[info] Count: 923062246
[info] Time: 4460
[info] Count: 923062246
@jdegoes
jdegoes / Advanced Functional Programming in Scala Training - ScalaWorld 2016.md
Created July 21, 2016 14:19
Advanced Functional Programming in Scala Training - ScalaWorld 2016
  1. Kiss type confusion goodbye as you learn to understand complex type signatures: T[_[_, _], _], T[({type λ[A]=F[A, K]})#λ].
  2. Level up your ability to write higher-order functions and define combinators to construct larger programs from smaller ones
  3. Learn how you can use rank-N types to program at a higher-level of abstraction, with strong correctness guarantees
  4. Discover how existentials help you compose functionality without exploding the size of type signatures
  5. Master type classes to generate generic, testable code without the tangling and non-local reasoning of inheritance
  6. Use "functional design patterns" like a boss, including functors, applicatives, monads, profunctors, monoids, and others
  7. Have your immutable cake and eat it too with "optics" that let you manipulate complex data structures with ease
  8. Traverse your own data structures without writing any recursive code through powerful, composable, generic recursion schemes
  9. Model effects with powerful, purely functional techniq
@DeLaGuardo
DeLaGuardo / pragmatapro-font-lock-symbols-v2.el
Last active February 28, 2024 01:27
Snippet for support ligatures from PragmataPro font in Emacs
;; Enable ligatures without prettify-symbols
(provide 'add-pragmatapro-symbol-keywords)
(defconst pragmatapro-fontlock-keywords-alist
(mapcar (lambda (regex-char-pair)
`(,(car regex-char-pair)
(0 (prog1 ()
(compose-region (match-beginning 1)
(match-end 1)
@rebcabin
rebcabin / kalmanSample.c
Last active April 22, 2020 18:16
Kalman filtering as a fold with CBLAS and LAPACK
/*
Copyright 2016 Brian C. Beckman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software

Explaining Miles's Magic

Miles Sabin recently opened a pull request fixing the infamous SI-2712. First off, this is remarkable and, if merged, will make everyone's life enormously easier. This is a bug that a lot of people hit often without even realizing it, and they just assume that either they did something wrong or the compiler is broken in some weird way. It is especially common for users of scalaz or cats.

But that's not what I wanted to write about. What I want to write about is the exact semantics of Miles's fix, because it does impose some very specific assumptions about the way that type constructors work, and understanding those assumptions is the key to getting the most of it his fix.

For starters, here is the sort of thing that SI-2712 affects:

def foo[F[_], A](fa: F[A]): String = fa.toString