Skip to content

Instantly share code, notes, and snippets.

View FermiDirak's full-sized avatar
🚴
gone cycling

Bryan Manuele FermiDirak

🚴
gone cycling
View GitHub Profile
struct MyStruct { /* ... */ }
impl MyStruct {
pub fn instance_method(&self) { /* ... */ };
}
// usage
let my_struct = MyStruct { /* ... */ }
my_struct.instance_method();
// user_model.rs
struct UserModel { /* ... */ }
impl UserModel {
pub fn fullname() -> String;
// ...
}
// happy_birthday_service.rs
pub fn send_message(session_context: &SessionContext, /* ... */) {
let message = session_context.user.happy_birthday_message();
// UserModel.ts
type UserModel = { /* ... */ }
namespace UserService {
function fullName(): string { /* ... */ };
// ...
}
// HappyBirthdayService.ts
function sendMessage(sessionContext: &SessionContext) {
let message = happyBirthdayMessage(sessionContext.user);
// consuming `apply_move` immutably
let chess_state = Chess::starting_board();
let player_move = Move { start: ('G', 1), end: ('F', 3) };
let next_state = {
let mut next_state = chess_state.clone();
next_state.apply_move(player_move);
next_state
};
// mutations are explicitly opt in
let mut chess_game = Chess::starting_board();
let player_move = Move { start: ('G', 1), end: ('F', 3) };
chess_game.apply_move(player_move);
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub enum PlayerSide { /* ... */ }
#[derive(Serialize, Deserialize)]
pub struct Move { /* ... */ }
#[derive(Serialize, Deserialize)]
pub struct Board { /* ... */ }
#[derive(Serialize, Deserialize)]
pub struct Chess { board: Board, turn: PlayerSide }
enum PlayerSide { /* ... */ }
type Move = { /* ... */ }
type Board = { /* ... */ }
type Chess = { board: Board; turn: PlayerSide }
namespace ChessService {
// functions for interacting with the game chess
function applyMove(chess: Chess, move: Move): Chess { /* ... */ }
function starting_state(): Chess { /* ... */ }
function listValidMoves(board: Chess, playerTurn: PlayerSide): Move[] { /* ... */ }
@FermiDirak
FermiDirak / reactathon2020.js
Created November 30, 2020 12:13
Learn static code analysis in React!
import jscodeshift from "jscodeshift";
import fs from "fs";
import path from "path";
const projectDirectory = "my/project/root/directory";
///////////////////////////////////////////////////////////////////////////////
// util functions
///////////////////////////////////////////////////////////////////////////////
import {getSourceFilesIterator, lsFiles} from "react-codestats";
const sourceFileIterator = getSourceFilesIterator("~/flexport/javascripts", {
extensions: ["js", "jsx"],
withParser: "flow",
});
let formulaOneImports = 0;
let formComponentImports = 0;
import iconNames from "latitude/iconNamesEnum";
import {getSourceFilesIterator} from "react-codestats";
const sourceFileIterator = getSourceFilesIterator("~/flexport/javascripts", {
extensions: ["js", "jsx"],
withParser: "flow",
});
const iconUsageCounts = iconNames.reduce((acc, iconName) => {
acc.set(iconName, 0);