Skip to content

Instantly share code, notes, and snippets.

View Lakret's full-sized avatar

Dmitry Slutsky ‮ ‮ Lakret

View GitHub Profile
@Lakret
Lakret / braces.rs
Created March 3, 2023 23:35
Matching Braces in Rust with a Stack
// you'll need to install lazy_static crate for this code to work, so put this into your Cargo.toml:
//
// [dependencies]
// lazy_static = "*"
#[macro_use]
extern crate lazy_static;
use std::collections::{HashMap, HashSet};
lazy_static! {
@Lakret
Lakret / nice_gen.py
Created December 22, 2021 17:55
Niceties Generator
import sys
from random import choice
parts = [
[
"Champ,",
"Fact:",
"Everybody says",
"Dang",
@Lakret
Lakret / clean_docker.exs
Last active August 29, 2020 13:15
Docker images cleanup
## This script removes all docker images with name `<none>`.
## Typically, this frees a lot of disk space if you often use different docker images.
## Removal is forced, i.e. `docker rmi -f` is used.
{output, 0} = System.cmd("docker", ["images"])
images =
output |> String.split("\n") |> Enum.drop(1) |> Enum.map(fn line ->
# since docker output is aligned by spaces, we need to do this trick here:
# we know that valid records in any column will not have repeated spaces,
use std::fmt;
use std::fmt::{Display, Formatter};
struct S {
x: i64,
}
fn define_impl() {
impl Display for S {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
use std::fmt;
use std::fmt::{Display, Formatter};
struct S {
x: i64,
}
fn define_impl() {
impl Display for S {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
@Lakret
Lakret / profiles.clj
Created August 6, 2019 10:02
Lein profile
{:user {:plugins [[cider/cider-nrepl "0.20.0"]
[mvxcvi/whidbey "2.1.1"]]
:middleware [whidbey.plugin/repl-pprint]
:dependencies [[org.clojure/tools.nrepl "0.2.13"]
[cljfmt "0.5.7"]
[com.bhauman/rebel-readline-cljs "0.1.4"]]
:aliases {"rebl" ["trampoline" "run" "-m" "rebel-readline-cljs.main"]}}}
@Lakret
Lakret / recursive_tuples_inlining.ex
Last active June 16, 2019 15:03
Recursive variable inlining in a macro
defmodule F do
@moduledoc """
Defines a macro that converts recursive 2-tuples to 3-tuples with the third
element being a sum of the first 2. Supports variable interpolation at any level
of nesting.
"""
defmacro f(quoted) do
inner(quoted)
end
@Lakret
Lakret / replace_macro.ex
Created June 14, 2019 12:51
Replace `^variable` with the variable value with Elixir macro
defmodule Foo do
defmacro select(columns) when is_list(columns) do
# in macro context we replace all `^foo` forms with `foo`
columns = Macro.postwalk(columns, &interpolate/1)
IO.inspect(columns, label: :columns_after_interpolate)
# we unquote in the caller context, and map atoms to Columns after all
# `^foo` were replaced with `foo`
quote do

Scala 10 million (with 1 million in memory):

def time[R](block: => R): R = {
    val t0 = System.nanoTime()
    val result = block
    val t1 = System.nanoTime()
    println("Elapsed time: " + (t1 - t0) / 1e9 + " s.")
    result
}
@Lakret
Lakret / chess.ex
Created February 28, 2019 01:17
Level 1. Improved naming
defmodule Chess do
alias Chess.{Board, Player, Move, Interaction}
def play() do
play(Board.init(), Player.first_turn(), 0)
end
def play(board, player, turn) do
move = Interaction.read_move(player)