Skip to content

Instantly share code, notes, and snippets.

View aleury's full-sized avatar

Adam Eury aleury

View GitHub Profile
@aleury
aleury / rust-yellow-belt-test.md
Last active September 11, 2024 14:26
Rust Yellow Belt Test

1. What does it mean to “move” a value in Rust? Contrast this with “borrowing” a value.

To "move" a value in Rust means to transfer "ownership" of that value to a new variable, where "ownership" refers to a system of rules that are enforced by the compiler:

  1. Each value in Rust has an owner.
  2. There can only be one owner at a time.
  3. When the owner goes out of scope, the value will be dropped.

Once a value as been moved to a new owner, accessing the value via its previous owner is no longer valid.

@aleury
aleury / README.md
Created April 25, 2024 16:50 — forked from nymous/README.md
Logging setup for FastAPI, Uvicorn and Structlog (with Datadog integration)

Logging setup for FastAPI

This logging setup configures Structlog to output pretty logs in development, and JSON log lines in production.

Then, you can use Structlog loggers or standard logging loggers, and they both will be processed by the Structlog pipeline (see the hello() endpoint for reference). That way any log generated by your dependencies will also be processed and enriched, even if they know nothing about Structlog!

Requests are assigned a correlation ID with the asgi-correlation-id middleware (either captured from incoming request or generated on the fly). All logs are linked to the correlation ID, and to the Datadog trace/span if instrumented. This data "global to the request" is stored in context vars, and automatically added to all logs produced during the request thanks to Structlog. You can add to these "global local variables" at any point in an endpoint with `structlog.contextvars.bind_contextvars(custom

@aleury
aleury / logs.txt
Last active January 12, 2024 06:29
Logs from databased back tests in GO.
➜ user git:(main) ✗ go test -v
Image: postgres:15.4
Host: 0.0.0.0:55020
ContainerID: 3284488a6e93
=== RUN Test_User
=== RUN Test_User/crud
dbtest.go:83: Waiting for database to be ready...
dbtest.go:101: Database ready
dbtest.go:116: Migrate and seed the database
dbtest.go:140: Ready for testing...
@aleury
aleury / linkedlist.go
Created September 8, 2023 12:09
Linked List in Go
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
type Node[T any] struct {
Value T
Next *Node[T]
}
@aleury
aleury / lexer.rs
Last active September 7, 2023 07:10
Rust lexer with state functions
struct Lexer;
impl Lexer {
pub fn emit(&mut self) {
todo!()
}
}
struct State(Option<fn(&mut Lexer) -> State>);
@aleury
aleury / yellow-belt.md
Last active August 4, 2023 14:51
BIT Yellow Belt Test

https://go.dev/ref/spec

So the yellow belt test is designed to get you comfortable with dipping into the spec and finding the info you need, as well as getting you familiar with some important language fundamentals. Here are your questions:

1. Describe the syntax and use of the defer keyword. When would it be useful? Briefly describe its connection with named result parameters.

The defer keyword applies to a function call and tells the Go runtime that the execution of the function should be deferred until the surrounding function returns, either because the function has an explicit return statement, it reaches the end of the function, or a panic occurs in the corresponding goroutine.

Basic usage of the defer keyword looks like

@aleury
aleury / password.clj
Created March 28, 2022 13:38
Clojure Password Generator
(def available-chars
(map char (range 33 123)))
(defn generate-password [length]
(str/join (take length (repeatedly #(rand-nth available-chars)))))

Domain Modeling with Tagged Unions in GraphQL, ReasonML, and TypeScript

GraphQL has exploded in popularity since its open-source announcement in 2015. For developers who had spent a lot of time managing data transformations from their back-end infrastructure to match front-end product needs, GraphQL felt like a tremendous step forwards. Gone were the days of hand-writing BFFs to manage problems of over-fetching.

A lot of value proposition arguments around GraphQL have been about over/under fetching, getting the data shape you ask for, etc. But I think GraphQL provides us more than that—it gives us an opportunity to raise the level of abstraction of our domain, and by doing so allow us to write more robust applications that accurately model the problems we face in the real world (changing requirements, one-off issues).

An underappreciated feature of GraphQL is its type system, and in particular features like [union types](https:

@aleury
aleury / fibs.ex
Created June 6, 2019 05:35
Elixir Cached Fibonacci Sequence
defmodule Cache do
def run(body) do
{:ok, pid} = Agent.start_link(fn -> %{0 => 0, 1 => 1} end)
result = body.(pid)
Agent.stop(pid)
result
end
def get_or_put(cache, key, if_not_found) do
Agent.get(cache, fn map -> map[key] end)