Skip to content

Instantly share code, notes, and snippets.

View pchiusano's full-sized avatar

Paul Chiusano pchiusano

View GitHub Profile
@pchiusano
pchiusano / monads.u
Last active April 27, 2024 08:18
Converting between algebraic effects and monads
-- This gist shows how we can use abilities to provide nicer syntax for any monad.
-- We can view abilities as "just" providing nicer syntax for working with the
-- free monad.
ability Monadic f where
eval : f a -> a
-- Here's a monad, encoded as a first-class value with
-- two polymorphic functions, `pure` and `bind`
type Monad f = Monad (forall a . a -> f a) (forall a b . f a -> (a -> f b) -> f b)
@pchiusano
pchiusano / Layout.elm
Created December 10, 2014 20:56
Annotated layout trees
module Elmz.Layout where
import List
import Array (Array)
import Either(..)
import Graphics.Element as E
import Graphics.Element (Direction, Element, Position)
type Pt = { x : Int, y: Int }
@pchiusano
pchiusano / Json.scala
Last active November 7, 2023 12:53
Simple JSON parser combinator library that does not use zippers
// WARNING! totally untested, I have only compiled the code! :)
package json
import collection.immutable.Map
import scalaz.{\/, MonadPlus}
import scalaz.\/._
import scalaz.std.vector._
import scalaz.std.map._
import scalaz.std.list._
@pchiusano
pchiusano / batching.u
Last active September 15, 2023 20:10
Batching transactions in durable storage
unique ability Batch where
increment : Nat -> ()
-- Like `State.transact`, but pauses the computation every `batchSize` calls to
-- `Batch.increment` and commits the work so far in one transaction, then resumes
-- the rest of the computation.
State.transact.batched : Nat -> Database -> '{Batch, Transaction, Exception} a ->{State, Exception} a
State.transact.batched batchSize db b =
go : Nat -> Request {Batch} a -> Either ('{Batch,Transaction,Exception} a) a
go acc = cases

Take-home functional programming interview

This document is licensed CC0.

These are some questions to give a sense of what you know about FP. This is more of a gauge of what you know, it's not necessarily expected that a single person will breeze through all questions. For each question, give your answer if you know it, say how long it took you, and say whether it was 'trivial', 'easy', 'medium', 'hard', or 'I don't know'. Give your answers in Haskell for the questions that involve code.

Please be honest, as the interviewer may do some spot checking with similar questions. It's not going to look good if you report a question as being 'trivial' but a similar question completely stumps you.

Here's a bit more guidance on how to use these labels:

@pchiusano
pchiusano / sequence.u
Created January 18, 2023 05:19
Sequence type in Unison supporting most operations in log_32(n) or O(1)
use data Array
structural type Sequence a
= Empty Nat
| Sequence Nat Nat (Array a) (Sequence (Array a)) (Array a)
Sequence.arity : Sequence a -> Nat
Sequence.arity = cases
Sequence.Empty arity -> arity
Sequence arity _ _ _ _ -> arity
@pchiusano
pchiusano / type-inhabitants.markdown
Last active January 7, 2023 17:23
Reasoning about type inhabitants in Haskell

This is material to go along with a 2014 Boston Haskell talk.

We are going to look at a series of type signatures in Haskell and explore how parametricity (or lack thereof) lets us constrain what a function is allowed to do.

Let's start with a decidedly non-generic function signature. What are the possible implementations of this function which typecheck?

wrangle :: Int -> Int
@pchiusano
pchiusano / updating.md
Last active December 14, 2022 17:10
Updating a dependency in Unison

Here's a transcript of me updating a dependency in Unison. We're going to make this more streamlined, but this is the process for now. First, I made a copy of the branch I was updating (in this case main of distributed):

.distributed> fork main topics.baseupdate

  Done.

Next I pulled the new version of base into lib.base_new:

@pchiusano
pchiusano / gen.u
Created December 8, 2022 17:04
Fancier `Gen`
unique type test.Result = Fail Failure | Ok Text
unique ability Weighted a where
bump : Nat -> ()
give : a -> ()
unique ability Gen where
generate : '{Weighted a} () -> a
Weighted.map : (a ->{g} b) -> '{Weighted a, g} r -> '{Weighted b,g} r
@pchiusano
pchiusano / suffixtree.u
Created November 22, 2022 23:01
Suffix tree prototyping
-- id is the doc id, a is the text type, v is the value type
unique type Trie id txt
= Remainder txt (Trie.Pos id)
| Branch Nat (Dataset Value (Trie.Pos id)) (Map txt (Value (Trie id txt)))
--Remainder id txt Nat
-- there can be multiple docs at this suffix
-- Branch size
--| Branch Nat (Dataset Value id) (Map txt (Value (Trie id txt)))
unique type Trie.Pos id = Pos id Nat