Skip to content

Instantly share code, notes, and snippets.

View quelgar's full-sized avatar
🇦🇺

Lachlan O'Dea quelgar

🇦🇺
View GitHub Profile
@quelgar
quelgar / Framing.scala
Last active March 15, 2019 02:55
A sink to frame a ZIO stream by delimited boundaries. (eg lines of text)
import scalaz.zio.console._
import scalaz.zio.stream._
import scalaz.zio.{Chunk, ZIO}
object Framing {
def lfDelimiter = Chunk('\n')
@quelgar
quelgar / typed_errors.md
Last active January 16, 2024 09:36
Every Argument for Static Typing Applies to Typed Errors

Every Argument for Static Typing Applies to Typed Errors

Think of all the arguments you've heard as to why static typing is desirable — every single one of those arguments applies equally well to using types to represent error conditions.

An odd thing I’ve observed about the Scala community is how many of its members believe that a) a language with a sophisticated static type system is very valuable; and b) that using types for error handling is basically a waste of time. If static types are useful—and if you like Scala, presumably you think they are—then using them to represent error conditions is also useful.

Here's a little secret of functional programming: errors aren't some special thing that operate under a different set of rules to everything else. Yes, there are a set of common patterns we group under the loose heading "error handling", but fundamentally we're just dealing with more values. Values that can have types associated with them. There's absolutely no reason why the benefits of static ty

@quelgar
quelgar / TRingBuffer.scala
Created May 23, 2022 05:34
A ring buffer implementation for ZIO STM
import zio.stm.*
import zio.prelude.*
final class TRingBuffer[A] private (array: TArray[A], stateRef: TRef[TRingBuffer.State]) {
import TRingBuffer.*
private def uncheckedRead(state: State): USTM[A] = array(state.start) <*
stateRef.set(state.copy(start = (state.start + 1) % capacity, size = state.size - 1))