(nodes: Node, kind: Kind)
| 1215 | } |
| 1216 | |
| 1217 | function 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 | |
| 1260 | function hasFailingGet(kind: Kind): boolean { |
| 1261 | return kind === "Prism" || kind === "Optional" |
no test coverage detected