Skip to content

Instantly share code, notes, and snippets.

💭
🦀

Aleksey Kladov matklad

💭
🦀
Block or report user

Report or block matklad

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@matklad
matklad / vfs.rs
Created Aug 21, 2019
VFS API for Rust
View vfs.rs
use std::path::Path;
/// VFS provides two benefits over using `fs:
///
/// * consistent snapshots: reading the same file twice is guranteed to return
/// the same result, unless you explicitly advance the revision of VFS
/// * automated file watching: if you read a file or a dir, you will be notified when the
/// file changes.
struct Vfs {}
@matklad
matklad / 01.py
Created Sep 28, 2018
decorators.py
View 01.py
def max(*args):
"""Finds the largest argument."""
print(f"max{args} = ...")
ret = 0
for x in args:
ret = ret if x < ret else x
print(f"max{args} = {ret}")
return ret
View alias.rs
extern crate alias;
use std::cell::Cell;
fn pairwise_diffs(xs: &mut [i32]) {
let xs: &[Cell<i32>] = alias::slice(xs);
// Iterating & mutating *without* indices!
for (x, y) in xs.iter().zip(xs[1..].iter()) {
x.set(y.get() - x.get())
}
View counters.rs
trait Counter: 'static {
fn create(data: &str) -> Self where Self: Sized;
}
struct CounterA(i32);
impl Counter for CounterA {
fn create(data: &str) -> Self where Self: Sized {
CounterA(data.parse::<i32>().unwrap())
}
}
View c.cpp
#include <vector>
#include <cstdint>
#include <iostream>
#include <algorithm>
void perm(std::vector<int32_t> const& xs, std::vector<int32_t> curr) {
if (curr.size() == xs.size()) {
for (int32_t x: curr) {
std::cout << x << ' ';
}
@matklad
matklad / idea.nix
Last active Jan 24, 2018
Using JetBrains JDK with Intellij IDEA on nixos
View idea.nix
# Put this file into `~/.config/nixpkgs/overlays/idea.nix`.
# Then, install IDEA with `nix-env -iA nixos.idea-community`.
self: super:
{
jbsdk = super.callPackage ~/config/nix/jbsdk.nix {}; # you might need to override this path.
idea-community = let
version = "2017.2.3";
View jbsdk.nix
pkgs.oraclejdk8.overrideDerivation (attrs: rec {
name = "oraclejdk-${productVersion}u${patchVersion}";
src = pkgs.fetchurl {
url = "https://bintray.com/jetbrains/intellij-jdk/download_file?file_path=jbsdk8u152b970.2_linux_x64.tar.gz";
sha256 = sha256_x86_64;
};
preUnpack = ''
mkdir src
pushd src
'';
View hm.fs
module HM
let rec loop x = loop x
let mk_dno x = loop loop
let assert_same_type a b =
(fun f -> // manually desuget let to avoid let-polymorphism
let _ = f a in
let _ = f b in
mk_dno loop
@matklad
matklad / result_iterator.rs
Last active Nov 13, 2017
Extension traits to work with Rust Iterators of Results conveniently
View result_iterator.rs
use result_iterator::ResultIterator;
use std::fs::{self, DirEntry};
use std::io;
use std::path::Path;
fn file_names(path: &Path) -> Result<Vec<String>, io::Error> {
let result = fs::read_dir(path)?
.map_then(|entry| {
let is_file = entry.file_type()?.is_file();
@matklad
matklad / a.xml
Created Jul 19, 2017
How `catch` identifier is used in Rust
View a.xml
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>file://$PROJECT_DIR$/swf-tree-0.0.3/src/avm1/actions.rs</file>
<line>103</line>
<entry_point TYPE="file" FQNAME="file://$PROJECT_DIR$/swf-tree-0.0.3/src/avm1/actions.rs" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Anonymous function parameter</problem_class>
<description>Interesting name</description>
</problem>
<problem>
You can’t perform that action at this time.