MCPcopy Create free account
hub / github.com/Effect-TS/effect / compileSet

Function compileSet

packages/effect/src/Optic.ts:1217–1258  ·  view source on GitHub ↗
(nodes: Node, kind: Kind)

Source from the content-addressed store, hash-verified

1215}
1216
1217function compileSet(nodes: Node, kind: Kind): (a: any, s: any) => any {
1218 if (hasSourceFreeSet(kind)) {
1219 return (a) => {
1220 for (let i = nodes.length - 1; i >= 0; i--) {
1221 a = nodes[i].set(a)
1222 }
1223 return a
1224 }
1225 }
1226 return (a, s) => {
1227 const len = nodes.length
1228 const sources = new Array(len)
1229 for (let i = 0; i < len; i++) {
1230 sources[i] = s
1231 const op = nodes[i]
1232 if (hasFailingGet(op.kind)) {
1233 const result = op.get(s)
1234 if (Result.isFailure(result)) {
1235 return result
1236 }
1237 s = result.success
1238 } else {
1239 s = op.get(s)
1240 }
1241 }
1242 for (let i = len - 1; i >= 0; i--) {
1243 const op = nodes[i]
1244 if (hasSourceFreeSet(op.kind)) {
1245 a = op.set(a)
1246 } else if (op.kind === "Lens") {
1247 a = op.set(a, sources[i])
1248 } else {
1249 const result = op.set(a, sources[i])
1250 if (Result.isFailure(result)) {
1251 return result
1252 }
1253 a = result.success
1254 }
1255 }
1256 return kind === "Optional" ? Result.succeed(a) : a
1257 }
1258}
1259
1260function hasFailingGet(kind: Kind): boolean {
1261 return kind === "Prism" || kind === "Optional"

Callers 1

makeFunction · 0.85

Calls 5

hasSourceFreeSetFunction · 0.85
hasFailingGetFunction · 0.85
setMethod · 0.65
getMethod · 0.65
succeedMethod · 0.45

Tested by

no test coverage detected