Skip to content

Instantly share code, notes, and snippets.

View stefanluptak's full-sized avatar

Stefan Luptak stefanluptak

View GitHub Profile
@stefanluptak
stefanluptak / early_exit.livemd
Created September 9, 2022 13:10
Early Exit in Elixir

Early exit

Section

defmodule Channel do
  def handle_event1("fetch-weather", _params, socket) do
    if socket.assigns[:target_date] do
      IO.puts("1 valid")

Datix LiveBook

Mix.install([
  {:datix, "~> 0.1.0"}
])

Update map in loop

Section

order = %{
  "_csrf_token" => "gwsgw",
  "custom_tags" => "",
  "id" => "62212",
  "products" => %{

Struct list to map list

Section

defmodule User do
  defstruct [:id, :name, :role, :age]
end
@stefanluptak
stefanluptak / hex_latest.ex
Created June 24, 2021 19:10
lib/mix/tasks/hex_latest.ex
defmodule Mix.Tasks.Hex.Latest do
@doc """
## Example usage:
`mix hex.latest jason`
Or you can copy the output with `pbcopy` like this:
`mix hex.latest jason | pbcopy`
"""
@stefanluptak
stefanluptak / benchmark.exs
Created April 19, 2020 15:24
Benchmarking performance of slicing text in different formats/ways
# 1024 bytes long string
string = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tempus iaculis erat. Cras orci enim, vulputate a elementum at, rutrum pulvinar sem. Donec gravida quam at lectus sagittis, eu facilisis felis scelerisque. Nulla quis leo quis ipsum sollicitudin pharetra. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec bibendum porttitor hendrerit. In hac habitasse platea dictumst.
Sed blandit massa ut lorem accumsan, ut suscipit erat aliquet. Ut vulputate sit amet ligula non auctor. Nullam pharetra vestibulum velit, lobortis ultrices ante blandit quis. Curabitur gravida fringilla enim non pulvinar. Vestibulum facilisis turpis ipsum, ac tempor magna varius at. Cras pulvinar mi in varius mattis. Curabitur viverra nibh est, eu consectetur magna euismod eget. Suspendisse potenti. Vestibulum hendrerit auctor dui id imperdiet. Quisque congue enim non eros lobortis, rhoncus aliquam lacus eleifend. Nam et malesuada mauris, et semper velit. Aenean volutpat cursus or
@stefanluptak
stefanluptak / behaviour.ex
Last active September 5, 2019 13:25
Behaviour vs. Protocol in Elixir
defmodule Counter do
@type t :: %__MODULE__{count: integer()}
defstruct count: 0
def transform(counter, %transformation_module{} = transformation) do
transformation_module.transform(counter, transformation)
end
end
defmodule Counter.Transformation do
#! /usr/bin/env elixir
ExUnit.start()
defmodule Ranges do
def run(indexes, length) do
ranges(indexes, length, nil, [])
end
# private
@stefanluptak
stefanluptak / FloatingViewController.swift
Last active September 29, 2017 08:56 — forked from mayoff/FloatingViewController.swift
Swift 4 version | A floating button on iOS for http://stackoverflow.com/q/34777558/77567
import UIKit
class FloatingButtonController: UIViewController {
private(set) var button: UIButton!
required init?(coder aDecoder: NSCoder) {
fatalError()
}

Keybase proof

I hereby claim:

  • I am eskimag on github.
  • I am eskimag (https://keybase.io/eskimag) on keybase.
  • I have a public key whose fingerprint is D7DF 3426 8D5A 3781 3F8B 5054 7128 89AF 8B9F 420D

To claim this, I am signing this object: