Skip to content

Instantly share code, notes, and snippets.

@fero23
fero23 / PoC
Last active February 3, 2016 19:08
Proof of concept of a new language
model Person {
uid: string(100),
name: string(200),
last_name: string(200),
birth_date: datetime
}
model Book {
name: string(200)
}
@fero23
fero23 / js_macro.rs
Created February 13, 2016 04:37
Rust macro to create a JS value
use std::collections::HashMap;
use std::convert::From;
enum JsType {
JsNum(i32),
JsString(String),
JsNullable(Option<Box<JsType>>),
JsObject {data: HashMap<String, JsType>, level: u32}
}
@fero23
fero23 / collection_conversion.rs
Last active March 7, 2016 00:13
Testing Rust collection conversions through FromIterator and IntoIterator
use std::iter::{Iterator, IntoIterator, FromIterator};
fn main() {
let v: Vec<i32> = FiveNumbers::new(2,3,5,8,13).into_iter().collect();
let f = v.iter().cloned().collect::<FiveNumbers>();
let r: Result<FiveNumbers, ()> =
vec![1,2,3,4,5].into_iter().map(|n| Ok(n)).collect();
let r2: Result<Vec<i32>, &str> =
f.clone().into_iter().map(|n| Ok(n)).collect();
let r3: Result<Vec<i32>, &str> =
@fero23
fero23 / rust_minesweeper_board_constructor.rs
Created March 10, 2016 05:17
A simple minesweeper board constructor using Rust
#[derive(Clone, Copy)]
enum CellType {
Number(u32),
Bomb,
Empty
}
type InputBoard = [[u8; 8];8];
type GameBoard = [[CellType; 8];8];
@fero23
fero23 / js_lib.rs
Created March 11, 2016 03:15
Sneak peek of a new Javascript builder library in Rust
use std::collections::HashMap;
use std::rc::Rc;
use std::fmt::Debug;
use std::ops::{Add, Sub};
#[derive(Debug, Clone)]
pub enum JsValue<T: ToString> {
StaticValue(T),
ResultValue(Box<JsOps>),
}
@fero23
fero23 / SimpleMinesweeper.cs
Last active December 15, 2022 17:04
Una implementación de buscaminas simple usando C# y Windows Forms
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
namespace SimpleMinesweeper
{
class Program
{
@fero23
fero23 / js_fn.rs
Created March 14, 2016 17:18
js_fn! macro initial design, declaration and usage (will not work standalone, these are just the key components)
#[derive(Debug)]
struct JsFunction<T: JsType, P> {
params_type: PhantomData<P>,
operations: Vec<Box<JsSentence>>,
return_value: Option<T>,
}
impl<S:Borrow<str>> From<S> for JsString {
fn from(value: S) -> JsString {
JsString::new(JsValue::StaticValue(value.borrow().to_string()))
@fero23
fero23 / seq_ops.rs
Last active January 10, 2023 10:05
Intersection and difference operations for iterators in Rust
pub trait IterOps<T, I>: IntoIterator<Item = T>
where I: IntoIterator<Item = T>,
T: PartialEq {
fn intersect(self, other: I) -> Vec<T>;
fn difference(self, other: I) -> Vec<T>;
}
impl<T, I> IterOps<T, I> for I
where I: IntoIterator<Item = T>,
T: PartialEq
@fero23
fero23 / quicksort.rs
Last active April 7, 2016 03:49
Quickstort implementation in Rust
fn main() {
let mut arr = [3,11,7,89,85,4,39,51,15,9,74,57,60,22,46,48,79,25,72,5,80,27];
arr.quicksort();
println!("{:?}", arr);
}
trait Sortable: PartialOrd {
fn quicksort(&mut self);
}
@fero23
fero23 / hkt.rs
Created April 8, 2016 20:55
Exploring the power of traits and associated types using a HKT trait definition
// Note to self: Remember that traits can store and reference
// a lot of type info related to the types that implemented it.
fn main() {
println!("{:?}", vec![2,4,5,6,8].map(|&n| n * 2));
println!("{:?}", vec![1,2,3,4,5].fold(0, |acc, &n| acc + n));
println!("{:?}", vec!["a", "b", "c"].fold_map(|c| c.to_uppercase()));
}
// Basically, for every container C<T> that implements this trait
// we save the information of his current type T, the type N to be