Skip to content

Instantly share code, notes, and snippets.

View Lisoph's full-sized avatar

Daniel Hauser Lisoph

  • Fronius International GmbH
  • Austria
  • 21:05 (UTC +02:00)
View GitHub Profile
fn _main?()
{
x = -10
y = +20
}
/*
#[yields]
fn foo(multi: i32) -> i32 {
let mut counter = 0i32;
yield 100;
println!("After yield 100!");
counter += 1;
yield 200;
let aeiou = "Hello!";
#![feature(conservative_impl_trait)]
fn take<'a>(take: &'a str) -> impl Fn(&'a str) -> (&'a str, &'a str) {
move |input| {
let len = input.chars().zip(take.chars()).take_while(|&(i, t)| i == t).count();
(&input[0..len], &input[len..])
}
}
fn chain<'a, F1, F2>(first: F1, second: F2) -> impl Fn(&'a str) -> ((&'a str, &'a str), &'a str)
@Lisoph
Lisoph / gui.rs
Created June 1, 2017 08:47
Rust GUI core concept
enum MouseButton {
Left,
Middle,
Right,
}
enum UiEvent {
MouseMove(i32, i32),
MouseClicked(i32, i32, MouseButton),
}
@Lisoph
Lisoph / event.rs
Created June 1, 2017 09:31
A Rust data structure that can store closures and invoke them. A poor mans version of `event` from C#.
struct Event<'a> {
handlers: Vec<Box<FnMut() + 'a>>,
}
impl<'a> Event<'a> {
fn new() -> Self {
Self {
handlers: Vec::new(),
}
}
@Lisoph
Lisoph / shared_widgets.rs
Last active June 7, 2017 09:03
Rust shared widgets
use std::collections::HashSet;
use std::cell::RefCell;
use std::rc::Rc;
struct WidgetRef(Rc<RefCell<Box<Widget>>>);
impl WidgetRef {
fn new(inner: Rc<RefCell<Box<Widget>>>) -> Self {
WidgetRef(inner)
}
@Lisoph
Lisoph / parser_combinators2.rs
Created October 4, 2017 09:19
Parser Combinators 2
#![feature(conservative_impl_trait)]
use std::cmp::PartialEq;
type ParseResult<T, I: Iterator<Item = T>> = Result<ParseStatus<T, I>, ParseError>;
#[derive(Debug)]
enum ParseStatus<T, I: Iterator<Item = T>> {
Ok(Vec<T>, I),
NeedMoreData(usize), // # of units needed
@Lisoph
Lisoph / main.rs
Created December 6, 2017 07:40
Advent of Code 2017 #3
#[derive(Debug, PartialEq, Copy, Clone)]
enum Dir {
Up,
Down,
Left,
Right,
}
#[derive(Clone, Copy, PartialEq)]
struct Point {
@Lisoph
Lisoph / main.rs
Created December 6, 2017 09:12
Advent of Code 2017 #4
fn main() {
let input = r#"pphsv ojtou brvhsj cer ntfhlra udeh ccgtyzc zoyzmh jum lugbnk
vxjnf fzqitnj uyfck blnl impo kxoow nngd worcm bdesehw
caibh nfuk kfnu llfdbz uxjty yxjut jcea
qiho qif eupwww avyglnj nxzotsu hio lws
xjty usocjsh pivk qnknunc yjcgh bwya djw zpyr
ycfmfe mgq sjiomg nfzjul bjwkmgu yvsnvgj dcjupu wzz blmn
rdowgbt vpwfdoi blzl laghnk gsa vhnpo cztxzlb rtz hvwonhb eciju pfjtbo
bqs bqs dbutvgf mmzb izpyud rap izpyud xlzeb mnj hjncs
xpu vwp nujcos piu irindir tpmfd umtvlm gznu
@Lisoph
Lisoph / main.rs
Created December 6, 2017 10:01
Advent of Code 2017 #1.2
fn main() {
let digits: Vec<_> = INPUT.chars().map(|c| char::to_digit(c, 10).unwrap()).collect();
let step = digits.len() / 2;
let mut acc = 0;
for (i, d) in digits.iter().enumerate() {
if let Some(x) = digits.iter().cycle().skip(i).nth(step) {
if d == x {
acc += d;
}