Skip to content

Instantly share code, notes, and snippets.

@azjezz
Last active August 19, 2025 16:40
Show Gist options
  • Save azjezz/bbc9e56b8624eade91a56095a1731652 to your computer and use it in GitHub Desktop.
Save azjezz/bbc9e56b8624eade91a56095a1731652 to your computer and use it in GitHub Desktop.
<?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