composition

How do I write combinators for my own parsers in Rust?

北战南征 提交于 2020-03-03 06:39:14
问题 Inspired by this video, I thought a little parser combinator library would be a good way to learn about strings, borrowing and typing in Rust—and it was so far. I managed to get a char parser and a digit parser to work: pub enum Parsed<'a, T> { Some(T, &'a str), None(&'a str), } impl<T> Parsed<'_, T> { // I was neither sure with the third & before the T... pub fn unwrap(&self) -> (&T, &str) { match self { // ... nor with the first one here. Parsed::Some(head, tail) => (&head, &tail), _ =>

How do I write combinators for my own parsers in Rust?

坚强是说给别人听的谎言 提交于 2020-03-03 06:32:27
问题 Inspired by this video, I thought a little parser combinator library would be a good way to learn about strings, borrowing and typing in Rust—and it was so far. I managed to get a char parser and a digit parser to work: pub enum Parsed<'a, T> { Some(T, &'a str), None(&'a str), } impl<T> Parsed<'_, T> { // I was neither sure with the third & before the T... pub fn unwrap(&self) -> (&T, &str) { match self { // ... nor with the first one here. Parsed::Some(head, tail) => (&head, &tail), _ =>

How to pass method result as parameter to base class constructor in C++?

£可爱£侵袭症+ 提交于 2020-01-24 19:05:30
问题 I've trying to achieve something like this: class Base { public: Base(string S) { ... }; } class Derived: Base { public: int foo; string bar() { return stringof(foo); // actually, something more complex }; Derived(int f) : foo(f), Base(bar()) { }; } Now, this doesn't work as I want, because bar() is called in the Derived constructor before foo is initialized. I considered adding a static function similar to bar() which takes foo as a parameter - and using that in the initialization list, but

Javascript Distinguish between Composition vs. Inheritance

[亡魂溺海] 提交于 2020-01-23 19:25:15
问题 in the classfull-Style (c++) or in the traditional Design Patterns (GofPatterns) it is really clear, what is the difference between composition and inheritance and how it is implemented and when to use what (advantages/disadvantages). But how do you distingish between Composition or the "normal" Inheritance in JS? or it is used as the same term? Is a prototype inheritance for example defined as a composition or inheritance? What are you guys thinking? 回答1: Is a prototype inheritance for

How to customize h:head when using ui:composition template?

£可爱£侵袭症+ 提交于 2020-01-19 09:56:08
问题 I am using JSF to render an HTML page. I design the page like it : <f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <meta name="language" content="fr" /> <title><ui:insert name="title">My app</ui:insert></title> </h:head> <h:body> <div id="top"> <ui:include src="

Refactoring legacy mixin-based class hierarchies

一曲冷凌霜 提交于 2020-01-19 04:04:05
问题 I'm currently working on a huge javascript project which has a huge class hierarchy and heavily uses mixins to extend functionality of base classes. Here is an example of how mixin looks like, we're using compose library to create class-like objects: // Base.js var Base = compose({ setX: function (x) { this.x = x; }, setY: function (y) { this.y = y; }, setPosition: function (x, y) { this.setX(x); this.setY(y); } }) // SameXAndY.js - mixin var SameXAndY = compose({ // Executes after setX in

Python function composition (max recursion depth error, scope?)

房东的猫 提交于 2020-01-17 01:10:36
问题 What is wrong with this function? It seems like a scope error (although I thought I had fixed that by placing each callable in the list, instead of using it directly). Error is max recursion depth reached (when calling comp(inv,dbl,inc))... Note: the question is: why is it even recursing, not why it's reaching the max depth... def comp(*funcs): if len(funcs) in (0,1): raise ValueError('need at least two functions to compose') # get most inner function composed = [] print("appending func 1")

Python function composition (max recursion depth error, scope?)

纵然是瞬间 提交于 2020-01-17 01:10:32
问题 What is wrong with this function? It seems like a scope error (although I thought I had fixed that by placing each callable in the list, instead of using it directly). Error is max recursion depth reached (when calling comp(inv,dbl,inc))... Note: the question is: why is it even recursing, not why it's reaching the max depth... def comp(*funcs): if len(funcs) in (0,1): raise ValueError('need at least two functions to compose') # get most inner function composed = [] print("appending func 1")

Cannot use constructors in open generic services with Kephas

走远了吗. 提交于 2020-01-15 09:21:11
问题 Somehow I cannot get to make it work having a service exported as open generic with an implementation with a constructor. I tried to add the [CompositionConstructor] attribute to it to no avail. It throws something like: System.Composition.Hosting.CompositionFailedException : No importing constructor was found on type 'MyType'. Any ideas? 回答1: Unfortunately this is a bug in the System.Composition library (check here https://github.com/dotnet/corefx/issues/40094). As I can see, this bug is

How to use __getattr__ to delegate methods to attribute?

Deadly 提交于 2020-01-15 06:27:49
问题 I have the following class: class MyInt: def __init__(self, v): if type(v) != int: raise ValueError('value must be an int') self.v = v def __getattr__(self, attr): return getattr(self.v, attr) i = MyInt(0) print(i + 1) I get the error: TypeError: unsupported operand type(s) for +: 'MyInt' and 'int' Shouldn't i.__add__(1) be called? And shouldn't __getattr__ be called when no such method is found in the MyInt class? 回答1: __getattr__ cannot be used to generate other magic methods. You'll need