juan_gandhi: (Default)
src

package main
 
import (
"fmt"
"strconv"
)
 
type Maybe[A any] interface {
GetOrElse(A) A
}
 
type Functor[A, B, C any] interface {
Map(func(A) B) C
}
 
type Some[A any] struct {
V A
}
 
func (s *Some[A]) GetOrElse(A) A {
return s.V
}
 
type SomeFunctor[A, B any] Some[A]
 
func (sa *SomeFunctor[A, B]) Map(op func(a A) B) Maybe[B] { return &Some[B]{op(sa.V)} }
 
type None[A any] struct{}
 
func (n *None[A]) GetOrElse(a A) A {
return a
}
 
type NoneFunctor[A, B any] None[A]
 
func (sa *NoneFunctor[A, B]) Map(op func(a A) B) Maybe[B] { return &None[B]{} }
 
func FindBy[X any, U ~[]X](src U, f func(X) bool) Maybe[X] {
for _, v := range src {
if f(v) {
return &Some[X]{v}
}
}
return &None[X]{}
}
 
func MaybeFunctor[A, B any](x Maybe[A]) Functor[A, B, Maybe[B]] {
switch v := x.(type) {
case *Some[A]:
return &SomeFunctor[A, B]{v.V}
case *None[A]:
return &NoneFunctor[A, B]{}
}
return nil
}
 
func EqF[X comparable](sent X) func(X) bool { return func(x X) bool { return sent == x } }
 
func main() {
src := []int{1, 2, 3}
fmt.Println(MaybeFunctor[int, string](FindBy(src, EqF(3))).Map(func(x int) string { return "val:" + strconv.Itoa(x) }).GetOrElse("n/a"))
fmt.Println(FindBy(src, EqF(5)).GetOrElse(-1))
}
 
juan_gandhi: (Default)
That's about free profunctors casually dropping into the code

 https://www.youtube.com/watch?v=2yRtxYbV1h4


juan_gandhi: (Default)
Вот простые два теста на Котлине.

    Stream.of<String>().forAll {   it.isEmpty() } shouldBe true
Stream.of<String>().forAll { !it.isEmpty() } shouldBe true



Не очень понятно, как это непрофессионалы воспримут. Моим студентам требуются усилия обычно.





juan_gandhi: (Default)
 

An Intuition for Optics

Link
juan_gandhi: (Default)
"крестьяне первым и неизменным условием какого бы то ни было соглашения ставили то, чтобы они не были принуждаемы к каким бы то ни было новым приемам хозяйства и к употреблению новых орудий
juan_gandhi: (Default)
 

70 Observe that, for any two streams, there is in general more than one acceptable order of interleaving. Thus, technically, ‘merge’ is a relation rather than a function—the answer is not a deterministic function of the inputs. We already mentioned (footnote 38 ) that nondeterminism is essential when dealing with concurrency. The merge relation illustrates the same essential nondeterminism, from the functional perspective.
 

71 The object model approximates the world by dividing it into separate pieces. The functional model does not modularize along object boundaries. The object model is useful when the unshared state of the ‘objects’ is much larger than the state that they share. An example of a place where the object viewpoint fails is quantum mechanics, where thinking of things as individual particles leads to paradoxes and confusions. Unifying the object view with the functional view may have little to do with programming, but rather with fundamental epistemological issues.

juan_gandhi: (Default)
Could not get rid of laziness, though.

package scalakittens.experiments

object CopyDL extends App {
/*
The problem is this: we have a linked list of nodes; each node has a value and a pointer to another node anywhere in the list.
We have to make a copy of this list.
Impossible without mutability or laziness. Haskell solution is laziness. Here's my solution.
*/

trait Node {
def value: Int
def next: Node
def random: Node
}

class DataNode(
val value: Int,
var next: Node,
var random: Node) extends Node

def inverseIndex(index: Array[Node]): Map[Node, Int] = {
index.indices map { i => index(i) -> i } toMap
}

def randIndex(index: Array[Node], inverseIndex: Map[Node, Int]): Array[Int] = {
index map (_.random) map inverseIndex
}

def copy(nodes: Iterable[Node]): Iterable[Node] = {
val index = nodes.toArray
val ii = inverseIndex(index).withDefaultValue(-1)

lazy val newNodes: List[IndirectNode] = index.indices map {
i =>
val old = index(i)
IndirectNode(old.value, ii(old.next), ii(old.random))
} toList

case class IndirectNode(value: Int, nextId: Int, rndId: Int) extends Node {
override def next = if (nextId < 0) null else newNodes(nextId)
override def random = if (rndId < 0) null else newNodes(rndId)

def valueOf(node: Node) = Option(node) map (_.value.toString) orNull

override def toString: String =
s"Node($value, ->${valueOf(next)}, ->${valueOf(random)})"
}

newNodes
}

val n1 = new DataNode(101, null, null)
val n2 = new DataNode(102, null, n1)
val n3 = new DataNode(103, n1, n2)
//n2.next = n3
n1.next = n2
n1.random = n3

val copied = copy(n1::n2::n3::Nil)
println(copied)

}

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

September 2025

S M T W T F S
 1 23456
78910111213
14151617181920
21222324252627
282930    

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 5th, 2025 08:45 pm
Powered by Dreamwidth Studios