Skip to content

Instantly share code, notes, and snippets.

View samdvr's full-sized avatar

Sam Davarnia samdvr

View GitHub Profile
This uses llm.datasette.io and OpenAI.
I use `git commit --template` to provide the output from the LLM to Git. This way, if you do not like the results, you
can quit your editor and no commit will be made.
# Shell function for generating a diff and editing it in your default editor:
gcllm() {
GIT_DIR="$(git rev-parse --git-dir)"
TEMPLATE="$GIT_DIR/COMMIT_EDITMSG_TEMPLATE"
@mpilquist
mpilquist / big3.scala
Last active September 25, 2019 00:40
Example encoding of Functor / Applicative / Monad using dotty 0.15
/* Example of encoding Functor/Applicative/Monad from cats with Dotty 0.15 features.
* Derived in part from Cats -- see https://github.com/typelevel/cats/blob/master/COPYING for full license & copyright.
*/
package structures
import scala.annotation._
trait Functor[F[_]] {
def (fa: F[A]) map[A, B](f: A => B): F[B]
def (fa: F[A]) as[A, B](b: B): F[B] =
@non
non / Wild.scala
Last active April 9, 2020 00:26
Interesting trick to lift existentials into values, so we can later refer to them.
package demo
import cats.{Applicative, Functor, Id, Semigroupal, Traverse}
import cats.arrow.FunctionK
/**
* Type-lifting operation to replace the wildcard type (i.e. _).
*
* In some cases we end up with code like: List[Option[_]]. This is
* fine unless you later need to write code in terms of a particular
@Icelandjack
Icelandjack / Yoneda.markdown
Last active February 10, 2023 00:06
Yoneda
import Data.Functor.Yoneda
import Data.Char
import Data.Kind

infixr 5
  ·

type  List :: (Type -> Type) -> Constraint
class List f where
@kiambogo
kiambogo / groupBy.scala
Last active September 22, 2020 00:01
FS2 1.0.0 groupBy
import fs2.concurrent.Queue
import cats.implicits._
import cats.effect.Concurrent
import cats.effect.concurrent.Ref
def groupBy[F[_], A, K](selector: A => F[K])(implicit F: Concurrent[F]): Pipe[F, A, (K, Stream[F, A])] = {
in =>
Stream.eval(Ref.of[F, Map[K, Queue[F, Option[A]]]](Map.empty)).flatMap { st =>
val cleanup = {
import alleycats.std.all._
@jdegoes
jdegoes / fpmax.scala
Created July 13, 2018 03:18
FP to the Max — Code Examples
package fpmax
import scala.util.Try
import scala.io.StdIn.readLine
object App0 {
def main: Unit = {
println("What is your name?")
val name = readLine()
@raulraja
raulraja / Tagless.kt
Last active January 20, 2020 15:53
Tagless in Kotlin with Arrow and manual DI
import arrow.Kind
import arrow.core.Option
import arrow.core.Try
import arrow.core.functor
import arrow.effects.IO
import arrow.effects.fix
import arrow.effects.functor
import arrow.typeclasses.Functor
/* algebras */
@johnynek
johnynek / scala path dependent serializers.scala
Last active July 17, 2018 21:16
Scala's path dependent types can be used to prove that a serialized value can be deserialized without having to resort to Try/Either/Option. This puts the serialized value into the type, so we can be sure we won't fail. This is very useful for distributed compute settings such as scalding or spark.
import scala.util.Try
object PathSerializer {
trait SerDe[A] {
// By using a path dependent type, we can be sure can deserialize without wrapping in Try
type Serialized
def ser(a: A): Serialized
def deser(s: Serialized): A
// If we convert to a generic type, in this case String, we forget if we can really deserialize

Quick Tips for Fast Code on the JVM

I was talking to a coworker recently about general techniques that almost always form the core of any effort to write very fast, down-to-the-metal hot path code on the JVM, and they pointed out that there really isn't a particularly good place to go for this information. It occurred to me that, really, I had more or less picked up all of it by word of mouth and experience, and there just aren't any good reference sources on the topic. So… here's my word of mouth.

This is by no means a comprehensive gist. It's also important to understand that the techniques that I outline in here are not 100% absolute either. Performance on the JVM is an incredibly complicated subject, and while there are rules that almost always hold true, the "almost" remains very salient. Also, for many or even most applications, there will be other techniques that I'm not mentioning which will have a greater impact. JMH, Java Flight Recorder, and a good profiler are your very best friend! Mea

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x