K

@kenbitanga

PHP: Stack Imlementation Using The Null Coalescing Operator (??)

PHP
2 years ago
<?php // methods below use the null coalescing operator (??) // instead of isset() and if-else statements class Node { public mixed $data; public ?Node $next;

C++ Interface, Polymorphism

C++
2 years ago
#include <iostream> struct IAnimal { virtual void eat() = 0; }; struct Dog: public IAnimal { void eat() { std::cout << "Dog eats\n"; }

C#: Interface, Polymorphism

C#
2 years ago
using System; public class Program { public static void Main(string[] args) { var dog = new Dog(); var cat = new Cat(); dog.eat(); cat.eat();

TS: Interface, Polymorphism

TypeScript
2 years ago
interface IAnimal { eat() : void; } class Dog implements IAnimal { eat() : void { console.log("Dog eats"); } }

Java: Interface, Polymorphism

Java
2 years ago
import java.util.*; import java.lang.*; import java.io.*; // The main method must be in a class named "Main". class Main { public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat();

PHP: Interface

PHP
2 years ago
<?php interface PandaInterface { public function eat(); public function poop(); public function sleep(); } class RedPanda implements PandaInterface

PHP: Abstract Classes

PHP
2 years ago
<?php abstract class Animal { abstract public function makeNoise(); } class Dog extends Animal { public function makeNoise() { echo 'Woof'. PHP_EOL; }

PHP: Constants, Class Constantts

PHP
2 years ago
<?php class Circle { const PI = 3.14159265359; public function circumference($diameter) { return $diameter * self::PI; }

PHP: Closures

PHP
2 years ago
<?php // Closures are anonymous functions // Create a math function. function math(Closure $type, $first, $second) { // Execute the closure with parameters return $type($first, $second); }

Ruby: Simple Linked List

Ruby
2 years ago
class Node attr_accessor :data, :next def initialize(data) @data = data @next = nil end end class Linkedlist @head

TS: Linked List, Namespace, Export Class

TypeScript
2 years ago
namespace SLL { class Node { constructor( public data, public next: Node = null, ) {} } // export class so it can be used outside namespace export class List { private he

PHP: Simple Linked List

PHP
2 years ago
<?php declare(strict_types=1); class Node { function __construct( public $data, public ?Node $next = null, ) {} }

TS: Constructor Promotion

TypeScript
2 years ago
namespace LinkedList { class Node { constructor( public data, public next: Node = null ) {} } const head = new Node(1);

Nodes

PHP
2 years ago
<?php class Node { // constructor promotion syntax function __construct( public $data, public ?Node $next = null, ) {} }

PHP: constructor promotion

PHP
2 years ago
<?php // regular constructor syntax class Dog { public string $name; public function __construct(string $name) { $this->name = $name; } }

PHP: array_map, array_filter, array_reduce

PHP
2 years ago
<?php $numbers = [1, 2, 3, 4, 5]; $numbers2 = [1, 2, 3, 4, 5]; // array_map $doubled = array_map(fn($n) => $n * 2, $numbers); // array_map, passing multiple arrays $x = array_map(fn($a, $b) => $a * $b, $numbers, $numbers2);

Namespace

TypeScript
2 years ago
namespace LinkedList { class Node { public data: number; public next: Node; constructor(data: number) { this.data = data; this.next = null; }

Linked List - Number

TypeScript
2 years ago
class ListNode<T> { public data: T; public next: ListNode<T>; constructor(data: T) { this.data = data; this.next = null; } } interface ILinkedList<T> {

DLList

TypeScript
2 years ago
class ListNode<T> { public next: ListNode<T> | null = null; public prev: ListNode<T> | null = null; constructor(public data: T) {} } interface ILinkedList<T> { insertInBegin(data: T): ListNode<T>; insertAtEnd(data: T): ListNode<T>; dele

Abstract Class

PHP
2 years ago
<?php // abstract class abstract class Greetings { // abstract method abstract public function greet(string $name) : string; // regular method public function bye(string $name) : string {