Last active
August 19, 2025 16:40
-
-
Save azjezz/bbc9e56b8624eade91a56095a1731652 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface Hash { | |
public function hash(Hasher $hasher): int; | |
} | |
interface ConcurrentHashMap<K: Hash, V> { | |
// ... | |
public function flatten<S>(): ConcurrentHashMap<K, S> | |
where | |
V: iterable<S> | |
; | |
public function mapConcurrently<R>((fn(K, V): R) $f): ConcurrentHashMap<K, R>; | |
public function awaitAll<O>(): ConcurrentHashMap<K, O> | |
where | |
V: Future<O> | |
; | |
} | |
class AmphpConcurrentHashMap<K: Hash, V> implements ConcurrentHashMap<K, V> { | |
// ... | |
public function mapConcurrently<R>((fn(K, V): R) $f): AmphpConcurrentHashMap<K, R> { | |
// .. | |
} | |
public function awaitAll<O>(): AmphpConcurrentHashMap<K, O> | |
where: | |
V: Future<O> | |
{ | |
// ... | |
} | |
} | |
function get_user_async(Id $id): Future<User> { | |
// ... | |
} | |
function get_users(Id ...$ids): ConcurrentHashMap<Id, User> { | |
$hm = new AmphpConcurrentHashMap::<Id, Future<User>>(); | |
foreach ($ids as $id) { | |
$hm->insert($id, get_user_async($id)); | |
} | |
return $hm->awaitAll::<User>(); | |
} | |
$users = get_users($id1, $id2); | |
$comments = $users | |
->map::<vec<Comment>>(fn($id, $user): vec<Comment> => $user->getComments()) | |
->flatten::<Comment>(); | |
// $comments -> vec<Comment> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment