Skip to content

Instantly share code, notes, and snippets.

View benjamingr's full-sized avatar
🖊️
Limited availability.

Benjamin Gruenbaum benjamingr

🖊️
Limited availability.
View GitHub Profile
var input = `
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
// About me:
// - Benji - Kid (Daniel), Dog (bimper), Wife (orit)
// - Excel Online client architecture @ Microsoft
// - Node.js project member - 7 years
// - A bunch of other open source stuff.
// - Magshimim since ±2014.
// - Taught startech (Nitzani Magshimim)
// - עקרונות מתקדמיים.
// - קורס פרוייקט for two years.
// - פיתוח תוכן / מנטור
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = &gpa.allocator();
var file = try std.fs.cwd().openFile("./sample.txt", .{});
const file_content = try file.readToEndAlloc(allocator.*, 1024 * 1024); // 1MB max read size
var iter = std.mem.split(u8, file_content, "\n");
defer allocator.free(file_content);
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = &gpa.allocator();
var file = try std.fs.cwd().openFile("./sample.txt", .{});
const file_content = try file.readToEndAlloc(allocator.*, 1024 * 1024); // 1MB max read size
var iter = std.mem.split(u8, file_content, "\n");
defer allocator.free(file_content);
import { default as parser } from 'stream-json';
import { Readable } from 'stream';
const parkingViolations = await fetch('https://s3.amazonaws.com/philadelphia-parking-violations-raw-data/parking_violations_2017.json');
await Readable.fromWeb(parkingViolations.body)
.pipe(parser())
.filter(violation => violation.name === 'stringChunk')
.map(x => x.value)
.take(5)
.forEach(v => console.log(v));
import { connect } from 'net';
import { once } from 'events';
import { Readable } from 'stream';
function* range(from, to) {
for(let i = from; i < to; i++) yield i;
}
async function tryToConnectToPort(port, host) {
const socket = connect(port, host);
try {
import { Readable, Transform, compose } from "stream";
{
// Before
const stream = new (class extends Readable {
constructor() {
super({ objectMode: true });
this.data = [1, 2, 3];
}
_read() {
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
async function* delayedRange() {
try {
for(let i = 1; i <= 1000; i++) {
await delay(100);
yield i;
}
} catch (e) {
// Let's say I have transaction code that looks like:
let state = 1;
session.withTransaction(async () => {
// this can retry, which means otherFn can run twice and possible insert the wrong value or in the wrong order
state++;
await Promise.all([
coll1.insertOne({ abc: 1 }, { session });
otherFn(state, session),
])
/*
Makes a channel that buffers up to n items
*/
function chan(n) {
const data = []; // data not yet read
const readersBacklog = []; // readers waiting for data
const writersBacklog = []; // writers waiting for data
let disposed = false;
// TODO(Benjamin) - disposing