Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.

Docs/field doc #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ trait FieldFactories:
self: Fields =>

export DefaultableInstances.given

def fromDefaultableExpr[A](expr: => A)(using d: Defaultable[A]): Field[A] =
Field.fromExpression(d.default)(expr)
def fromIntExpr(expr: => Int): Field[Int] =

def fromInt(expr: => Int): Field[Int] =
fromDefaultableExpr[Int](expr)

def fromDouble(expr: => Double): Field[Double] =
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/io/github/rustfields/field/FieldOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import cats.syntax.all.*

import scala.annotation.targetName

/**
* This trait defines additional syntax and operations with [[io.github.rustfields.field.Fields]]
*/
trait FieldOps:
self: Fields =>

Expand Down
13 changes: 10 additions & 3 deletions src/main/scala/io/github/rustfields/field/Fields.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.github.rustfields.field
import cats.Monad
import io.github.rustfields.lang.FieldCalculusSyntax

import scala.annotation.tailrec
import scala.language.implicitConversions

trait Fields:
Expand Down Expand Up @@ -96,8 +95,16 @@ trait Fields:
*/
def fold[A](f: Field[A])(z: A)(aggr: (A, A) => A): A =
f.getMap.values.fold(z)(aggr)

def fold[A](f: Field[A])(aggr: (A, A) => A)(using d: Defaultable[A]): A =

/**
* * Folds the elements of the field using the given aggregation function and an implicit default value as initial value for the aggregation. The traversal order is not specified.
* @param f the field
* @param aggr the aggregation function
* @param d the [[Defaultable]] instance
* @tparam A the type of the field
* @return the result of applying the fold operator op between all the elements and d, or d if this collection is empty.
*/
def foldDef[A](f: Field[A])(aggr: (A, A) => A)(using d: Defaultable[A]): A =
fold(f)(d.default)(aggr)

def flattenField[A](ff: Field[Field[A]]): Field[A] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait FieldLib:

def foldhoodfDef[A](aggr: (A, A) => A)(init: => Field[A])(using d: Defaultable[A]): A =
vm.nest(FoldHood(vm.index))(write = true) {
Field.fold(init.neighbouring)(aggr)
Field.foldDef(init.neighbouring)(aggr)
}

/**
Expand Down