Skip to content

Instantly share code, notes, and snippets.

@Savelenko
Savelenko / EarlyReturnExperiment.fs
Created September 28, 2023 14:26
Early return computation expression in F#
(* An experiment with implementing early return in F#. Initial examples work, but I do not know whether it is really correct! *)
/// A computation which runs to completion and produces 'r or returns an 'a early, short-circuiting some steps.
type R<'a,'r> =
private
| Done of 'r
| EarlyReturn of 'a
| Zero
| Effect of (unit -> R<'a,'r>)
import SwiftUI
struct ContentView: View {
@State var path: [String] = []
func navigationButton(value: String) -> some View {
NavigationButton {
path.append(value)
} label: {
Text("NavigationButton")
@mr-salty
mr-salty / clean-bazel-cache.sh
Last active January 21, 2024 21:55
script to clean up files in the bazel cache
#! /bin/bash -e
#
# clean up everything in CACHE_DIR last accessed more than DAYS days ago.
# also removes files with bogus timestamps in the future.
#
# -----------------------------------------------------------------------
# Copyright 2021 Todd Derr (todd.derr@gmail.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@Savelenko
Savelenko / ExistentialEngines.fs
Last active June 27, 2024 18:57
F# existential types
// The engine interface and helpers
type IEngine<'a,'b> =
abstract member Capacity : int // Does not depend on 'a or 'b; just an example
// Some other stuff here possible depending on 'a and 'b
/// A function which works on any engine polymorphically and returns a result of type 'r'. Needed because
/// F# does not support higher-ranked types (not the same as HKT!) in regular functions. This does work in members.
type EngineFunction<'r> =
abstract member Apply : IEngine<'a,'b> -> 'r
@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
//
// PSPDFFastEnumeration.h
// PSPDFFoundation
//
// PSPDFKit is the leading cross-platform solution for integrating PDFs into your apps: https://pspdfkit.com.
// Try it today using our free PDF Viewer app: https://pdfviewer.io/
//
// This file is MIT licensed.
@protocol PSPDFFastEnumeration <NSFastEnumeration>
@bmc
bmc / FS2ReadInputStream.scala
Last active July 28, 2019 11:04
Read a CSV-like, pipe-delimited (jar) resource into Person records, via FS2
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import java.util.Date
import java.text.SimpleDateFormat
import fs2._
// id and gender should really be more strongly-typed, but
// this is just a demo...
case class Person(id: Int,
firstName: String,
@gallais
gallais / Vector.ml
Last active October 5, 2018 23:46
Length-indexed lists
(*
We don't really care about the data constructors here
but they are needed, cf:
https://sympa.inria.fr/sympa/arc/caml-list/2013-10/msg00190.html
*)
type zero = Zero
type 'a succ = Succ
(*
[] has length 0

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
@mandubian
mandubian / gist:0fd090c0f75a46346f5e7898eeac9e28
Last active September 15, 2017 15:11
Improving compile-time for structure based on implicits resolutions