Skip to content

Instantly share code, notes, and snippets.

@gvolpe
gvolpe / IsUUID.scala
Last active July 22, 2022 16:17
Scala 3 custom newtypes
import java.util.UUID
import monocle.Iso
trait IsUUID[A]:
def iso: Iso[UUID, A]
object IsUUID:
def apply[A](using ev: IsUUID[A]): IsUUID[A] = ev

Description of Program

I have a program which looks roughly like this

  1. Waits for some files
  2. When they arrive, parses them to extract data
  3. Reads database state
  4. Performs calculation
  5. Saves new state to database
  6. Generates emails on the basis of computed data
@gvolpe
gvolpe / shared-state-in-fp.md
Last active March 15, 2022 20:27
Shared State in pure Functional Programming

Shared State in pure Functional Programming

Newcomers to Functional Programming are often very confused about the proper way to share state without breaking purity and end up having a mix of pure and impure code that defeats the purpose of having pure FP code in the first place.

Reason why I decided to write up a beginner friendly guide :)

Use Case

We have a program that runs three computations at the same time and updates the internal state to keep track of the

import cats.implicits._
import cats.{ Applicative, Monad }
// Операции над контекстом
trait ContextWriter[F[_]] {
def put(key: String, value: String): F[Unit]
}
@limansky
limansky / MapReader.scala
Last active June 25, 2017 09:39
Read Map[String, String] to case class
package me.limansky
import cats.Monoid
import shapeless.labelled.{FieldType, field}
import shapeless.{::, HList, HNil, LabelledGeneric, Lazy, Witness}
trait MapReader[T] {
def read(m: Map[String, String]): T
}

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
@pathikrit
pathikrit / NQueen.scala
Last active January 19, 2023 21:30
O(n!) solution to the n-Queen puzzle (https://en.wikipedia.org/wiki/Eight_queens_puzzle)
/**
* Solves the n-Queen puzzle in O(n!)
* Let p[r] be the column of the queen on the rth row (must be exactly 1 queen per row)
* There also must be exactly 1 queen per column and hence p must be a permuation of (0 until n)
* There must be n distinct (col + diag) and n distinct (col - diag) for each queen (else bishop attacks)
* @return returns a Iterator of solutions
* Each solution is an array p of length n such that p[i] is the column of the queen on the ith row
*/
def nQueens(n: Int): Iterator[Seq[Int]] =
(0 until n)
@betehess
betehess / scalaz.irclog
Last active November 29, 2017 09:06
On freenode/#scalaz this morning
<RaceCondition> can I use Scalaz to get exhaustion checks when matching on numeric values? Scala obviously doesn't do that
<RaceCondition> ! 1.1 match { case x if 0.0 <= x && x < 0.5 => "bad"; case x if 0.5 <= x && x <= 1.0 => "good" }
<dibblego> doubt it
<multibot_> scala.MatchError: 1.1 (of class java.lang.Double)
<multibot_> ... 38 elided
<dibblego> use types though?
<RaceCondition> wdym?
<dibblego> use a type to note each range
<dibblego> you want a floating-point between 0.0 and 1.0?
<RaceCondition> wouldn't that just move the problem to a different stage?
@mwjackson
mwjackson / gist:4475177
Created January 7, 2013 13:58
SM init code
public class StructureMapConfiguration
{
public static void Initialise(IEnumerable<Assembly> assembliesToScan, params Action<IInitializationExpression>[] extraConfigs)
{
ObjectFactory.Initialize(config =>
{
config.Scan(scan =>
{
foreach(var assembly in assembliesToScan)
{
@orclev
orclev / trie.hs
Created February 28, 2012 04:23
Haskell trie implementation
import Data.Maybe
import Control.Monad (liftM)
import Data.List (isPrefixOf)
import qualified Data.Map as M
import qualified Data.Foldable as F
-- | Trie container data type
data Trie a = Trie { value :: Maybe a
, children :: M.Map Char (Trie a) }
deriving (Show)