composition

In Python, are mixins equivalent to composition? If so, then why not just use composition?

↘锁芯ラ 提交于 2021-02-18 22:36:02
问题 I understand mixin as what looks like inheritance but what is more like composition. (edit: I tend to think giving additional functionality/attributes by mixin rather than giving another is-a relationship .) Mentally, I'm saying something like this when I use mixin: I'm giving you this mixin you are missing, rather than you are actually this mixin-type as well.(is-a) And I read few times, you should prefer composition over inheritance. We could just use straight compositions instead of mixins

In Python, are mixins equivalent to composition? If so, then why not just use composition?

荒凉一梦 提交于 2021-02-18 22:35:19
问题 I understand mixin as what looks like inheritance but what is more like composition. (edit: I tend to think giving additional functionality/attributes by mixin rather than giving another is-a relationship .) Mentally, I'm saying something like this when I use mixin: I'm giving you this mixin you are missing, rather than you are actually this mixin-type as well.(is-a) And I read few times, you should prefer composition over inheritance. We could just use straight compositions instead of mixins

Python: A better way to write n compositions of a function?

做~自己de王妃 提交于 2021-02-18 18:05:38
问题 I wrote a function "rep" that takes a function f and takes n compositions of f. So rep(square,3) behaves like this: square(square(square(x))). And when I pass 3 into it, rep(square,3)(3)=6561. There is no problem with my code, but I was wondering if there was a way to make it "prettier" (or shorter) without having to call another function or import anything. Thanks! def compose1(f, g): """Return a function h, such that h(x) = f(g(x)).""" def h(x): return f(g(x)) return h def rep(f,n): newfunc

Python: A better way to write n compositions of a function?

怎甘沉沦 提交于 2021-02-18 18:04:21
问题 I wrote a function "rep" that takes a function f and takes n compositions of f. So rep(square,3) behaves like this: square(square(square(x))). And when I pass 3 into it, rep(square,3)(3)=6561. There is no problem with my code, but I was wondering if there was a way to make it "prettier" (or shorter) without having to call another function or import anything. Thanks! def compose1(f, g): """Return a function h, such that h(x) = f(g(x)).""" def h(x): return f(g(x)) return h def rep(f,n): newfunc

Python: A better way to write n compositions of a function?

萝らか妹 提交于 2021-02-18 18:03:08
问题 I wrote a function "rep" that takes a function f and takes n compositions of f. So rep(square,3) behaves like this: square(square(square(x))). And when I pass 3 into it, rep(square,3)(3)=6561. There is no problem with my code, but I was wondering if there was a way to make it "prettier" (or shorter) without having to call another function or import anything. Thanks! def compose1(f, g): """Return a function h, such that h(x) = f(g(x)).""" def h(x): return f(g(x)) return h def rep(f,n): newfunc

TypeScript: class composition

一笑奈何 提交于 2021-02-18 05:23:51
问题 Based on this awesome Composition over Inheritance video by MPJ, I've been trying to formulate composition in TypeScript. I want to compose classes , not objects or factory functions. Here is my effort so far (with a little help from lodash ): class Barker { constructor(private state) {} bark() { console.log(`Woof, I am ${this.state.name}`); } } class Driver { constructor(private state) {} drive() { this.state.position = this.state.position + this.state.speed; } } class Killer { constructor

Simulating field inheritence with composition

陌路散爱 提交于 2021-02-17 05:04:34
问题 I have several pairs of structs for which the fields of one is a perfect superset of the other. I'd like to simulate some kind of inheritance so I don't have to have separate cases for each struct since that would almost double my code. In a language like C, I could simulate inheritance of fields with something like this: struct A { int a; }; struct B { struct A parent; int b; }; main() { struct B test1; struct A *test2 = &test1; test2->a = 7; } I want to do something like this in Rust. I

How to compose polymorphic functions in Haskell? [duplicate]

半世苍凉 提交于 2021-02-08 11:29:40
问题 This question already has an answer here : What is the monomorphism restriction? (1 answer) Closed last month . I have the following file: module SimpleComposition where class Domain a where f :: a -> a g :: a -> a h = f . g When trying to loading it in ghci, I get the error src\play.hs:7:5: error: * No instance for (Domain c0) arising from a use of `f' * In the first argument of `(.)', namely `f' In the expression: f . g In an equation for `h': h = f . g I believe the problem is that the

How to compose polymorphic functions in Haskell? [duplicate]

◇◆丶佛笑我妖孽 提交于 2021-02-08 11:29:18
问题 This question already has an answer here : What is the monomorphism restriction? (1 answer) Closed last month . I have the following file: module SimpleComposition where class Domain a where f :: a -> a g :: a -> a h = f . g When trying to loading it in ghci, I get the error src\play.hs:7:5: error: * No instance for (Domain c0) arising from a use of `f' * In the first argument of `(.)', namely `f' In the expression: f . g In an equation for `h': h = f . g I believe the problem is that the

What is delegation in julia?

纵然是瞬间 提交于 2021-02-08 11:12:31
问题 I see occational references to delegation, or the delegation design pattern in Julia. What is this? E.g. I see it mentioned in This file in DataStructures.jl 回答1: This is a form of polymorphism via composition (rather than inheritance) Say one has a wrapper type, wrapping some instance of a concrete subtype of AbstractT where the wrapper itself is intended to be a subtype of AbstractT (Not nesc always true, but in general). To add all the methods one exacts such a subtype of AbstractT to have