juan_gandhi: (Default)
[personal profile] juan_gandhi
isFinite tells us whether we can cache it; maybe it's coming from a predicate that checks the size, or something.


class Cache[K,V](isFinite: Boolean, build: K => V):
  private val cache: mutable.Map[K, V] = mutable.Map[K, V]()

  private val app: K => V = if (isFinite) (k: K) => cache.getOrElseUpdate(k, build(k)) else (k: K) => build(k)
  
  inline def apply(k: K): V = app(k)

Date: 2025-05-03 04:47 pm (UTC)
From: [personal profile] aklepatc
Понятно о чём это. Однако бесконечные последовательности тоже можно кешировать - кусками. (А вычислять лениво часто стоит и конечные тоже).
Edited (Исправление.) Date: 2025-05-03 04:55 pm (UTC)

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

May 2025

S M T W T F S
    1 2 3
456 7 8 9 10
11 121314151617
18192021222324
25262728293031

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 17th, 2025 10:08 am
Powered by Dreamwidth Studios
OSZAR »