Skip to content

Instantly share code, notes, and snippets.

View pchiusano's full-sized avatar

Paul Chiusano pchiusano

View GitHub Profile

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 / pubring.asc
Created February 5, 2016 16:58
My public PGP key
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BCPG v1.51
mQENBFa008UBCACp9mGyIyJ4+56TtMFNQLcUJwDd9I+I1Xt5rEL8k58GKEpsunWp
P1N7vfZOr0UcLtm+bKTNsKYiIMztqZAJVLEIRlQc1/3l53Bpwozzf3Ta+XZ6pIzG
RjNyVGyufKRmpyk6fy8bZ97/ZjmwuKM/1NbCC2HKY5UIOUhVhoFwnqQQRCEOZmme
/2eCamGE6KdDblXo7b+oobBtkZUvBCr/RIspGDQaxyyghttwaaCWDUKf/wV1JTDw
47bn8nt5149YONXor/AIoAzwNWw6dsYNRklfSPQARVJ5AAG5uK+s3nyUe4KJHUll
TKLRgTKBgyFPhfcILNCb4BFUS+92w2tLepcLABEBAAG0J1BhdWwgQ2hpdXNhbm8g
PHBhdWwuY2hpdXNhbm9AZ21haWwuY29tPokBHAQTAQIABgUCVrTTxQAKCRDgp4oY
@pchiusano
pchiusano / id_rsa.pub
Created February 5, 2016 16:38
My public RSA key
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCl7vQYUMJCI3w3nMae/ojbMttxioFhRl8irKMnhVYq7clNvvY9vNZchtOP0eX/a+qh31/WI8OA98p9bMALX4PU0D2GI123e6Yn5TxUdY9GaizHwuw5w0CbkDWiHmRQUq7HZHOit0qw146Dx00StdENccdUtBrPEPaUY5Z/fb7rvJW2CdCX956QaJMm1T3TTGFUfU+Lm1NsnQzw+eL1P7o2ePrf4wXLb90y6/k43VmZyIyRN1XdR8AKXemaLNh2INOCRlFTxGknJyJD4HTzPKfAptkyFi3nCjyHzgKz1zHhvsw4OEEdQ3Nbzxwx5TE1vSd/XfMbrvHtRgwIoR536N8F [email protected]
@pchiusano
pchiusano / Ref.scala
Created January 22, 2016 15:34
Transactionally updatable references
package fs2.internal
import java.util.concurrent.atomic._
/** A reference which may be updated transactionally, without use of `==` on `A`. */
private[fs2] class Ref[A](id: AtomicLong, ref: AtomicReference[A]) {
/**
* Obtain a snapshot of the current value, and a setter
* for updating the value. The setter may noop (in which case `false`
* is returned) if another concurrent call to `access` uses its
@pchiusano
pchiusano / process1.scala
Created January 21, 2016 01:20
Implementation of liftFirst for FS2
def liftFirst[F[_],A,B,C](f: Process1[A,B]): Process1[(A,C),(B,C)] = {
def go(cs: Chunk[C], s: Stepper[A,B]): Handle[Pure,(A,C)] => Pull[Pure,(B,C),Unit] = h =>
s.step match {
case Stepper.Done => Pull.done
case Stepper.Fail(err) => Pull.fail(err)
case Stepper.Emits(chunk, next) =>
if (cs.isEmpty) go(cs, next)(h)
else {
val last = cs(cs.size - 1)
@pchiusano
pchiusano / Semaphore.scala
Last active December 10, 2015 16:39
Asynchronous semaphores in FS2
package fs2.async.mutable
import fs2.async.AsyncExt
/** A nonblocking semaphore, useful as a concurrency primitive. */
trait Semaphore[F[_]] {
/** Returns the number of permits currently available for acquisition. Always nonnegative. */
def available: F[Long]
@pchiusano
pchiusano / diamonds.scala
Created December 8, 2015 20:06
Design of splitting combinators in FS2
package fs2
import util.Monad
import Step._
import java.util.concurrent.atomic.AtomicLong
object diamond {
/**
* Pass elements of `s` through both `f` and `g`, then combine the two resulting streams.
@pchiusano
pchiusano / reflex-helpers.hs
Last active November 24, 2015 22:18
Handy Reflex function for running IO actions
module ReflexHelpers where
import Control.Monad.IO.Class
import Reflex
import Reflex.Dom
evaluate :: (MonadWidget t m, Reflex t) => (a -> IO b) -> Event t a -> m (Event t b)
evaluate f actions = performEvent $ fmap (liftIO . f) actions
now :: (MonadWidget t m, Reflex t) => a -> m (Event t a)
@pchiusano
pchiusano / LinkedMap.scala
Created November 12, 2015 16:33
Simple purely functional map that efficiently tracks insertion order
/**
* A Map which tracks the insertion order of entries, so that entries may be
* traversed in the order they were inserted. Uses just two purely functional
* maps.
*/
import scala.collection.immutable.LongMap
class LinkedMap[K,V](
entries: Map[K,(V,Long)],
@pchiusano
pchiusano / override.hs
Created November 6, 2015 01:53
Overriding the keyboard arrows in a Reflex text box
searchbox <- textInput def
let isArrow i = i == 38 || i == 40
let tweak e = e >>= \i -> if isArrow i then i <$ preventDefault else pure i
tweakedKeydown <- wrapDomEvent (_textInput_element searchbox) elementOnkeydown (tweak getKeyEvent)
_ <- widgetHold (pure ()) (fmap (const (pure ())) tweakedKeydown)