Skip to content

Instantly share code, notes, and snippets.

View lovasoa's full-sized avatar
🎯
Focusing

Ophir LOJKINE lovasoa

🎯
Focusing
View GitHub Profile
@lovasoa
lovasoa / generic-factorial.rs
Created April 11, 2019 09:40
generic factorial function in rust
use num::{One, BigUint}; // 0.2.0
use std::ops::{MulAssign, AddAssign};
fn fac<T>(n: T) -> T
where T: One + AddAssign + Ord,
for <'a> T : MulAssign<&'a T> {
let mut r = T::one();
let mut i = T::one();
while i <= n {
/*
A WBO (https://wbo.openode.io) hello world script
This draws an orange square
Be careful not to commit more than a few updates per second to the server when scripting, or it will ban you.
Enter the following in your browser's javascript console:
*/
@lovasoa
lovasoa / piper.rs
Last active September 12, 2019 07:49
rust : convert a writer function to an io::Read
extern crate pipe;
/**
Some libraries (such as handlebars or serde) offer functions that can generate data when given
an object that implements io::Write.
Some other libraries (such as Rocket) can consume data only from objects implementing io::Read.
Here is an example `piper` function that can be used to make these two kinds of libraries together.
private static PrimitiveIterator.OfInt alphaChars(CharSequence s) {
return s.chars().filter(Character::isAlphabetic).map(Character::toLowerCase).iterator();
}
public static boolean compareAlphabeticCharacters(CharSequence a, CharSequence b) {
PrimitiveIterator.OfInt aStream = alphaChars(a);
PrimitiveIterator.OfInt bStream = alphaChars(b);
while (aStream.hasNext() && bStream.hasNext()) {
if (aStream.nextInt() != bStream.nextInt()) return false;
}
package com.qwant;
import org.ccil.cowan.tagsoup.jaxp.SAXParserImpl;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
package com.company;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.stream.Collectors.joining;
public class Main {
public static String decode(String morseCode) {
package com.company;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
public class Main {
static void hist(String codes, PrintStream out) {
Map<Character, Integer> map = new HashMap<>();
@lovasoa
lovasoa / memoize_async.js
Last active January 25, 2020 01:09
ES6 async memoize, using async/await and a Map to store the cache.
function memoize_async(func) {
const m = async function (key) {
if (!m.cache.has(key)) m.cache.set(key, await func.apply(this, arguments));
return m.cache.get(key);
};
m.cache = new Map();
return m;
};
@lovasoa
lovasoa / test.java
Last active September 26, 2018 16:41
package com.company;
import java.io.PrintWriter;
import java.sql.SQLException;
import static org.junit.Assert.assertEquals;
public class MainTest {
@org.junit.Test