Skip to content

Instantly share code, notes, and snippets.

View zoldar's full-sized avatar

Adrian Gruntkowski zoldar

View GitHub Profile
@zoldar
zoldar / money.erl
Last active December 27, 2019 19:07
add(M1, M2) ->
case {M1, M2} of
{{money, Cents1, Currency}, {money, Cents2, Currency1}} ->
{ok, {money, Cents1 + Cents2, Currency1}};
{{money, _, C1}, {money, _, C2}} ->
{error, {currencies_mismatch, C1, C2}}
end.
@zoldar
zoldar / client.ex.diff
Created March 29, 2020 18:22
myxql optimize for larger payloads
diff --git lib/myxql/client.ex lib/myxql/client.ex
index 54309c2..769f956 100644
--- lib/myxql/client.ex
+++ lib/myxql/client.ex
@@ -169,7 +169,7 @@ defmodule MyXQL.Client do
end
def recv_packets(client, decoder, decoder_state, timeout \\ :infinity) do
- case recv_data(client, timeout) do
+ case recv_data(client, 0, timeout) do
defmodule Project.AbsintheAddDeprecateDirective do
@moduledoc """
This pipeline modifier is a first, local attempt at potentially fixing
https://github.com/absinthe-graphql/absinthe/pull/1045 upstream.
The phase is inserted just before Directives expansion.
Deprecation directives are built if `deprecation` attribute is present on
the node (e.g. a FieldDefinition).
It's not entirely clear if instantiating the Directive and its Arguments
is the canonical way of doing it - to be verified with upstream devs.
"""

Advent of Code 2022

Day 1

input = """
2494
8013
1055
5425
import std/tables
var points = 0
let choicePoints = {
'R': 1,
'P': 2,
'S': 3
}.toTable
import std/sequtils, std/sets, std/tables, std/os, std/strformat
let priorities = concat(
zip(toSeq('a'..'z'), toSeq(1..26)),
zip(toSeq('A'..'Z'), toSeq(27..52))
).toTable
iterator chunks[T](a: seq[T], size: int): seq[T] =
var chunk: seq[T] = @[]
import std/sequtils, std/os, std/strformat, std/strscans, std/sets, sugar
proc countPairs(input: seq[string], fun: (HashSet[int], HashSet[int]) -> bool): int =
var count = 0
for line in input:
let (ok, lower1, upper1, lower2, upper2) = scanTuple(
line, "$i-$i,$i-$i")
if ok:
import std/strformat, std/sequtils, std/strscans, std/strutils, sugar
proc extractInput*(input: seq[string]): (seq[string], seq[string]) =
runnableExamples:
let (s, i) = extractInput(@["foo", "bar", "", "baz", "bax"])
assert s == @["foo", "bar"]
assert i == @["baz", "bax"]
var storage: seq[string]
var instructions: seq[string]
import std/strformat, std/sets
proc findMarker(input: string, length: int): int =
for idx in 0..<input.len:
if input[idx..<idx+length].toHashSet.len == length:
result = idx + length
break
proc findStartOfPacket*(input: string): int =
findMarker(input, 4)
import std/sequtils, std/strutils, std/sugar, std/strformat
type
NodeKind = enum File, Dir
Node = object
kind: NodeKind
name: string
size: int
children: seq[Node]