Skip to content

Instantly share code, notes, and snippets.

@runarorama
runarorama / gist:a8fab38e473fafa0921d
Last active April 13, 2021 22:28
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]
@fljot
fljot / Mac OS X 10_5_ Windows Ctrl.xml
Last active January 9, 2023 07:12
AutoHotkey mappings to emulate OSX keyboard shortcuts on Windows
<!-- put this to IDEA keymaps config folder. For v13 it is <userdir>\.IntelliJIdea13\config\keymaps\ -->
<?xml version="1.0" encoding="UTF-8"?>
<keymap version="1" name="Mac OS X 10.5+ Windows Ctrl" parent="Mac OS X 10.5+">
<action id="$Copy">
<keyboard-shortcut first-keystroke="meta C" />
<keyboard-shortcut first-keystroke="meta INSERT" />
<keyboard-shortcut first-keystroke="control C" />
<keyboard-shortcut first-keystroke="control INSERT" />
</action>
<action id="$Cut">
/**
* Free applicative functors
*/
sealed trait FreeA[F[_],A] {
/**
* The canonical monoidal natural transformation that interprets
* this free program by giving it the semantics of the applicative functor `G`.
*/
def foldMap[G[_]:Applicative](f: F ~> G): G[A] = this match {
case Pure(x) => Applicative[G].pure(x)
@milessabin
milessabin / gist:cadd73b7756fe4097ca0
Last active September 16, 2019 13:44
A new approach to encoding dependently-typed chained implicits, using singleton types ...
object Demo {
// A couple of type classes with type members ...
trait Foo[T] {
type A
}
object Foo {
implicit val fooIS = new Foo[Int] { type A = String }
}
@djspiewak
djspiewak / streams-tutorial.md
Created March 22, 2015 19:55
Introduction to scalaz-stream

Introduction to scalaz-stream

Every application ever written can be viewed as some sort of transformation on data. Data can come from different sources, such as a network or a file or user input or the Large Hadron Collider. It can come from many sources all at once to be merged and aggregated in interesting ways, and it can be produced into many different output sinks, such as a network or files or graphical user interfaces. You might produce your output all at once, as a big data dump at the end of the world (right before your program shuts down), or you might produce it more incrementally. Every application fits into this model.

The scalaz-stream project is an attempt to make it easy to construct, test and scale programs that fit within this model (which is to say, everything). It does this by providing an abstraction around a "stream" of data, which is really just this notion of some number of data being sequentially pulled out of some unspecified data source. On top of this abstraction, sca

@viktorklang
viktorklang / Gistard.scala
Last active June 9, 2017 07:27
Gistard — an sbt autoplugin for depending on Gists — such as Gistard itself
/*
Copyright 2015 Viktor Klang
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
@milessabin
milessabin / gist:a212d946aef33811fee1
Last active August 29, 2015 14:20
Functional update of common fields of a sealed family of case classes via a case-class-like copy through the common super type ...
import shapeless._
/**
* Functional update of common fields of a sealed family of case classes
* via a case-class-like copy through the common super type ...
*/
object BaseCopyDemo extends App {
import copySyntax._
// Sealed family of case classes ...
@milessabin
milessabin / gist:c3c9fbc57b1d913c2ee4
Last active December 2, 2020 11:37
Merging arbitrary case classes via shapeless ... (LabelledGeneric and record merge).
import shapeless._
object CaseClassMergeDemo extends App {
import mergeSyntax._
case class Foo(i: Int, s: String, b: Boolean)
case class Bar(b: Boolean, s: String)
val foo = Foo(23, "foo", true)
val bar = Bar(false, "bar")
@milessabin
milessabin / gist:b16e0765e1acdc90fd29
Last active August 16, 2016 16:25
Functional update of common fields of an open family of case classes via a case-class-like copy through the common super type ...
import shapeless._
/**
* Functional update of common fields of an *open* family of case classes
* via a case-class-like copy through the common super type ...
*
* This is a follow up to my earlier post showin how to do functional
* update for a sealed family of case classes,
*
* https://gist.github.com/milessabin/a212d946aef33811fee1
def multicast[A](p1: Process[Task, A], bound: Int = 10): Process[Task, Process[Task, A]] = Process suspend {
val queues = async signalOf Set[async.mutable.Queue[A]]()
def publish(a: A): Task[Unit] = for {
qsList <- queues.discrete filter { s => !s.isEmpty } take 1 runLog
qs = qsList flatMap { _.toList }
_ <- Task gatherUnordered (qs.toList map { _ enqueueOne a })
} yield ()