Skip to content

Instantly share code, notes, and snippets.

View greenrd's full-sized avatar

Robin Green greenrd

View GitHub Profile
@katylava
katylava / git-selective-merge.md
Last active February 27, 2024 10:18
git selective merge

Update 2022: git checkout -p <other-branch> is basically a shortcut for all this.

FYI This was written in 2010, though I guess people still find it useful at least as of 2021. I haven't had to do it ever again, so if it goes out of date I probably won't know.

Example: You have a branch refactor that is quite different from master. You can't merge all of the commits, or even every hunk in any single commit or master will break, but you have made a lot of improvements there that you would like to bring over to master.

Note: This will not preserve the original change authors. Only use if necessary, or if you don't mind losing that information, or if you are only merging your own work.

@aloiscochard
aloiscochard / Stream.scala
Created November 2, 2011 16:06
Scala [In]finite Stream Constructor
import Stream._
/** A possibly finite stream that repeatedly applies a given function to a start value.
*
* @param start the start value of the stream
* @param f the function that's repeatedly applied
* @return the stream returning the possibly finite sequence of values `start, f(start), f(f(start)), ...`
*/
def iterate[A](f: A => A, a: A): Stream[A] = unfold((x: A) => Some((x, f(x))), a)
@milessabin
milessabin / gist:2659013
Created May 11, 2012 11:08
Proving equality of type constructors in Scala via an "arbitrary object" encoding of universal quantification.
// Universal quantification is encoded in terms of quantifier-free
// assertions about an "abitrary" type (cp. "all swans are white" vs.
// "the arbitrary swan is white". Inspired by Kit Fine's 1985 "Reasoning
// with Arbitrary Objects", http://philosophy.fas.nyu.edu/object/kitfine.
//
// Possibly also related to Oleg Kiselyov's "Interpreting types as
// abstract values", http://okmij.org/ftp/Computation/index.html#teval.
// What I wouldn't give for kind-polymorphism here ...
@pthariensflame
pthariensflame / IndexedState.md
Last active June 15, 2022 18:42
An introduction to the indexed state monad in Haskell, Scala, and C#.

The Indexed State Monad in Haskell, Scala, and C#

Have you ever had to write code that made a complex series of succesive modifications to a single piece of mutable state? (Almost certainly yes.)

Did you ever wish you could make the compiler tell you if a particular operation on the state was illegal at a given point in the modifications? (If you're a fan of static typing, probably yes.)

If that's the case, the indexed state monad can help!

Motivation

@larsrh
larsrh / set.scala
Created May 2, 2013 06:47
Sets are not Functors
scala> :paste
// Entering paste mode (ctrl-D to finish)
case class A(x: Int)(y: Int) { def toB = B(x, y) }
case class B(x: Int, y: Int) { def toA = A(x)(y) }
// Exiting paste mode, now interpreting.
defined class A
defined class B
@joechrysler
joechrysler / who_is_my_mummy.sh
Last active February 26, 2024 04:06
Find the nearest parent branch of the current git branch
#!/usr/bin/env zsh
git show-branch -a \
| grep '\*' \
| grep -v `git rev-parse --abbrev-ref HEAD` \
| head -n1 \
| sed 's/.*\[\(.*\)\].*/\1/' \
| sed 's/[\^~].*//'
# How it works:
@akiomik
akiomik / .gitattributes
Created December 19, 2013 15:30
gitattributes for scala
*.scala diff=scala
@uarun
uarun / logback.xml
Last active October 6, 2015 15:11
Slick 2.0 - Enabling SQL Statement Logging
<logger name="scala.slick.jdbc.JdbcBackend.statement" level="DEBUG" />
@paulp
paulp / gist:9081797
Created February 18, 2014 22:29
Classic.
scala> val buf = ListBuffer(1)
buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1)
scala> val xs = buf.toIterable match { case xs: List[Int] => xs }
xs: List[Int] = List(1)
scala> buf ++= 1 to 100
res11: buf.type = ListBuffer(1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)
scala> xs
import scala.collection.mutable.ListBuffer
class Ummutable[T] private (buf: ListBuffer[T]) {
def this(xs: T*) = this(ListBuffer() ++= xs)
def append(ys: T*): this.type = { buf ++= ys ; this }
final val xs: List[T] = buf.toIterable match { case xs: List[T] => xs }
override def toString = s"Ummutable(xs = ${xs mkString ", "})"
}