K

@kenbitanga

Swift: Enums (Suit, Rank)

Swift
2 months ago
enum Suit { case spades, hearts, diamonds, clubs var icon: Character { switch self { case .spades: return "♤" case .hearts: return "♡" case .diamonds: return "♢"

Swift: Enums (Suit, Rank)

Swift
2 months ago
enum Suit { case spades, hearts, diamonds, clubs var icon: Character { switch self { case .spades: return "♤" case .hearts: return "♡" case .diamonds: return "♢"

Rust: Deque (Option<*mut Node<T>>, Cursor, split_before cursor)

Rust
4 months ago
#![allow(dead_code)] use std::any; use std::fmt; use std::marker::PhantomData; type Link<T> = Option<*mut Node<T>>; struct Node<T> { elem: T,

Tuple Structs

Rust
6 months ago
struct Color(u8, u8, u8); fn main() { let red = Color(255, 0, 0); println!("{}, {}, {}", red.0, red.1, red.2) }

Enums

Rust
6 months ago
enum Direction { Up, Down, Left, Right, } fn main() { let player_direction: Direction = Direction::Down;

Structs

Rust
6 months ago
struct Color { red: u8, green: u8, blue: u8, } fn main() { let mut bg = Color { red: 100, green: 200, blue: 50 }; bg.red = 255;

Go: Download Goroutine

Go
8 months ago
package main import ( "fmt" "time" ) func download(ch chan string) { time.Sleep(1 * time.Second) ch <- "Download complete!"

Go: Concurrency, Goroutines, Channels

Go
8 months ago
package main import ( "fmt" "time" ) func fetchData(ch chan string) { time.Sleep(1 * time.Second) ch <- "Download complete!"

JS: Inheritance using Constructor Functions

NodeJS
8 months ago
function Base() { this.a = 100 } function Derived() { Base.call(this) this.b = 200 } Base.prototype.info = function () { console.log(this.a, this.b) }

JS: Inheritance using Constructor Functions

NodeJS
8 months ago
function Base() { this.a = 100 } function Derived() { Base.call(this) this.b = 200 } Object.setPrototypeOf(Derived.prototype, Base.prototype);

JS: Set Prototype of an Object

NodeJS
8 months ago
log = console.log let a = { info() { return this.val } } let b = { val: 200, __proto__: a }

JS: Inheritance using Constructor Functions

NodeJS
8 months ago
// Inheritance using constructor functions function BaseFunction() {} function DerivedFunction() {} // set prototypal inheritance chain Object.setPrototypeOf(DerivedFunction.prototype, BaseFunction.prototype) let f = new DerivedFunction() console.log(f)

JS: Inheritance using Classes vs Constructor Functions

NodeJS
8 months ago
// Inheritance using classes class BaseClass {} class DerivedClass extends BaseClass {} let c = new DerivedClass() console.log(c) console.log(c.__proto__) console.log(c.__proto__.__proto__) console.log(c.__proto__.__proto__.__proto__)

JS: Creating a private variable using closure

NodeJS
8 months ago
function counter() { let count = 0 return { increment() { console.log(++count) }, decrement() { console.log(--count) }

JS: Building longer prototype chains

NodeJS
8 months ago
function Base() {} function Derived() {} // Set the `[[Prototype]]` of `Derived.prototype` // to `Base.prototype` // Don't use Object.create() as its error prone Object.setPrototypeOf(Derived.prototype, Base.prototype); const obj = new Derived(); // obj ---> Derived.prototype ---> Base.prototype ---> Object.prototype ---> null

JS: Inheritance using Constructor Functions

NodeJS
8 months ago
// constructor function function Animal(name) { this.name = name } // add speak() method to Animal.prototype // so they can be inherited Animal.prototype.speak = function () { console.log(`${this.name} can speak.`) }

JS: Class Inheritance

NodeJS
8 months ago
class Person { constructor(name, age) { this.name = name this.age = age } greet() { console.log(`Hello, ${this.name}`) } }

JS: Inheritance using Constructor Functions

NodeJS
8 months ago
// inheritance using // constructor functions // constructor function function Person(name, age) { this.name = name this.age = age } // add greet() method to Person prototype

TS: DI Example

TypeScript
8 months ago
interface IMessageService { sendMessage(): void } // a service that // implements IMessageService class EmailService implements IMessageService { sendMessage(): void { console.log("Email sent!") }

Python: Recursive Factorial with Auxiliary Function and Accumulating Parameter

Python
2 years ago
# recusive factorial with an auxiliary function (go) # and an accumulating parameter (acc) def fac(n): def go(n, acc=1): if n == 0: return acc return go(n-1, acc*n) return go(n) # driver code print(fac(5))