Questions tagged [composition]

Composition is the process of combining, or composing, parts of a program into a larger program.

1737 questions
13
votes
3 answers

How to use composition instead of inheritance when sharing logic between components in Angular 6?

I have a module in Angular that is structured likes this: moduleName componentA componentB Now componentA and componentB are very similar, as they share some attributes and methods, e.g.: protected available: boolean = true; As I don't…
13
votes
3 answers

How to pass in an instance variable from a React component to its HOC?

I typically use component composition to reuse logic the React way. For example, here is a simplified version on how I would add interaction logic to a component. In this case I would make CanvasElement selectable: CanvasElement.js import React, {…
13
votes
8 answers

Meta-composition during music performances

A couple of weeks ago, my piano teacher and I were bouncing ideas off of each other concerning meta-composing music software. The idea was this: There is a system taking midi input from a bunch of instruments, and pushes output to the speakers and…
mindvirus
  • 5,016
  • 4
  • 29
  • 46
13
votes
5 answers

Where and why is identity function useful?

I understand why function composition is important. It allows building large and complex functions from small and simple ones. val f: A => B = ... val g: B => C = ... val h = f andThen g; // compose f and g This composition conforms to identity…
Michael
  • 41,026
  • 70
  • 193
  • 341
13
votes
2 answers

Tacit function composition in Haskell

Say I have a mean function defined like so: mean xs = sum xs / (fromIntegral $ length xs) but I want it in some tacit form, like this: mean = sum / (fromIntegral . length) Is there a built-in Haskell way to do something along these lines without…
Benjamin Kovach
  • 3,190
  • 1
  • 24
  • 38
12
votes
3 answers

How to write without Do notation

I was playing around with composable failures and managed to write a function with the signature getPerson :: IO (Maybe Person) where a Person is: data Person = Person String Int deriving Show It works and I've written it in the do-notation as…
Dave
  • 257
  • 2
  • 7
12
votes
3 answers

Using self-defined Cython code from other Cython code

I am currently trying to optimize my Python program and got started with Cython in order to reduce the function calling overhead and perhaps later on include optimized C-libraries functions. So I ran into the first problem: I am using composition in…
packoman
  • 1,230
  • 1
  • 16
  • 36
12
votes
4 answers

OOP Reuse without Inheritance: How "real-world" practical is this?

This article describes an approach to OOP I find interesting: What if objects exist as encapsulations, and the communicate via messages? What if code re-use has nothing to do with inheritance, but uses composition, delegation, even …
yretuta
  • 7,963
  • 17
  • 80
  • 151
12
votes
3 answers

Composition vs Inner Classes

What are the differences and the similarities between composition and inner classes? I am trying to learn the principles of Java and try to figure out the whole image. For me is better to make analogies and to see the differences and similarities…
Alexis
  • 498
  • 4
  • 13
12
votes
2 answers

How implement composition in php

I want to implement next fragment of diagram, using PHP. See composition example diagram below: We can implement composition in Java using inner classes. But there is no analog of "inner class" in PHP. Of course, there are traits. But we can use…
profport
  • 131
  • 1
  • 1
  • 7
12
votes
1 answer

React.js: Composing components to create tabs

I’m trying to make a tabs component. TabsSwitcher and TabsPanel must be separate components so they could be used anywhere in DOM, e.g. TabsSwitcher doesn’t have to be followed by TabsPanel. To make it work, I need to somehow connect these…
NVI
  • 14,907
  • 16
  • 65
  • 104
12
votes
3 answers

Function "composition" and type safety in java

I'm trying to implement a class that isolates a part of a object, and replaces the part with with something else. The part of the object may not have the same type as the object itself. A simple example would be a class that takes the string…
loopbackbee
  • 21,962
  • 10
  • 62
  • 97
11
votes
1 answer

`does` versus `but` operators when mixing in a Role into an object in Raku

If I have a Role R defined as: role R { method answer { 42 } } What is the difference (if any) between these two lines: my $a = 'question' does R; my $b = 'question' but R; They appear very similar: say $a.answer; # OUTPUT: «42» say $b.answer; …
codesections
  • 8,900
  • 16
  • 50
11
votes
2 answers

Combining fragments of Haskell Code to get the bigger picture

This is the code that I came upon somewhere but want to know how this works: findIndices :: (a -> Bool) -> [a] -> [Int] findIndices _ [] = [] findIndices pred xs = map fst (filter (pred . snd) (zip [0..] xs)) Output: findIndices (== 0)…
11
votes
3 answers

Extending socket.socket with a new attribute

I wish to extend Python socket.socket with a new attribute (in particular, a queue.Queue, but could be anything else). I am trying to decide whether I should use inheritance or composition, but at some point both pose a problem I am not sure how to…
zeycus
  • 860
  • 1
  • 8
  • 20