The following are appendices from Optics By Example, a comprehensive guide to optics from beginner to advanced! If you like the content below, there's plenty more where that came from; pick up the book!
| <?php | |
| $num = floatval($argv[1] ?? '1e6'); | |
| echo "iterations: $num\n"; | |
| function noop() { | |
| } | |
| $start = microtime(true); | |
| printf("starting raw at:\t\t%.3F s\n", $start); |
| -- 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) |
| {-# LANGUAGE DataKinds #-} | |
| -- | My database API. | |
| module DBAPI where | |
| import Data.Defaults | |
| data ConnSpec p = ConnSpec | |
| { username :: !(Required p String) |
| {-# LANGUAGE LambdaCase #-} | |
| {-# LANGUAGE NumericUnderscores #-} | |
| {-# LANGUAGE OverloadedStrings #-} | |
| {-# LANGUAGE ScopedTypeVariables #-} | |
| {-# LANGUAGE TypeApplications #-} | |
| -- | | |
| -- To reproduce with curl, run mainTls (from ghci) and use | |
| -- | |
| -- @ |
This tutorial explains how Unison handles 'effectful' computations, like storing state or performing I/O, using abilities. It assumes you haven't come across abilities before, and covers everything from the ground up.
This is an unofficial tutorial, written before the one on unisonweb.org/docs. The approach taken here is slow and methodical. Your first stop should be the official tutorial, if you haven't seen it already.
This doc is a Unison transcript - the source is here.
Terminology note: other languages with ability systems typically call them 'effect handlers' or 'algebraic effects', but many of the ideas are the same.
| #!/usr/bin/env stack | |
| -- stack --resolver lts-14.20 --install-ghc runghc --package either --package free | |
| {-# LANGUAGE DeriveFunctor #-} | |
| module Main | |
| where | |
| import Control.Applicative.Free | |
| import Data.Bifunctor (first) | |
| import Data.Either.Combinators (maybeToRight) | |
| import Data.Either.Validation |
It’s folklore that if you’re summing a list of numbers, then you should always use strict foldl. Is that really true though? foldr is useful for lists when the function we use is lazy in its second argument. For (+) :: Int -> Int -> Int this is tyically not the case, but in some sense that’s because Int is “too strict”. An alternative representation of numbers is to represent them inductively. If we do this, sumation can be lazy, and foldr can do things that foldl simply can’t!
First, let’s define natural numbers inductively, and say how to add them:
data Nat = Zero | OnePlus Nat deriving Show
one :: Nat| select | |
| first_name, | |
| last_name | |
| from | |
| users | |
| left join | |
| companies on companies.id = users.company_id | |
| where ( | |
| companies.name like 'TERM%' or | |
| first_name like 'TERM%' or |
| /* | |
| * MIT License | |
| * | |
| * Copyright (c) 2023-2024 Fabio Lima | |
| * | |
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| * of this software and associated documentation files (the "Software"), to deal | |
| * in the Software without restriction, including without limitation the rights | |
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| * copies of the Software, and to permit persons to whom the Software is |