Skip to main content

If you do this long enough, you'll eventually find yourself writing this function over and over:

public static Type3 CombineFunc1AndFunc2( Func<Type1, Type2> func1, Func<Type2, Type3>> func2, Type1 input) { return func2(func1(input)) } 

Congratulations, you've invented function composition.

Wrapper functions like this don't have much use when they're specialized to one type. However, if you introduce some type variables and omit the input parameter, then your GetFormattedRate definition looks like this:

public static Func<A, C> Compose( Func<B, C> outer, Func<A, B>> inner) { return (input) => func2outer(func1inner(input)) } var GetFormattedRate = Compose(FormatRate, GetRate); var formattedRate = GetFormattedRate(rateKey); 

As it stands, what you're doing has little purpose. It's not generic, so you need to duplicate that code all over the place. It overcomplicates your code because now your code has to assemble everything it needs from a thousand tiny functions on its own. Your heart's in the right place though: you just need to get used to using these sorts of generic higher order functions to put things together. Or, use a good old fashion lambda to turn Func<A, B> and A into Func<B>.

Don't repeat yourself.

If you do this long enough, you'll eventually find yourself writing this function over and over:

public static Type3 CombineFunc1AndFunc2( Func<Type1, Type2> func1, Func<Type2, Type3>> func2, Type1 input) { return func2(func1(input)) } 

Congratulations, you've invented function composition.

Wrapper functions like this don't have much use when they're specialized to one type. However, if you introduce some type variables and omit the input parameter, then your GetFormattedRate definition looks like this:

public static Func<A, C> Compose( Func<B, C> outer, Func<A, B>> inner) { return (input) => func2(func1(input)) } var GetFormattedRate = Compose(FormatRate, GetRate); var formattedRate = GetFormattedRate(rateKey); 

As it stands, what you're doing has little purpose. It's not generic, so you need to duplicate that code all over the place. It overcomplicates your code because now your code has to assemble everything it needs from a thousand tiny functions on its own. Your heart's in the right place though: you just need to get used to using these sorts of generic higher order functions to put things together. Or, use a good old fashion lambda to turn Func<A, B> and A into Func<B>.

Don't repeat yourself.

If you do this long enough, you'll eventually find yourself writing this function over and over:

public static Type3 CombineFunc1AndFunc2( Func<Type1, Type2> func1, Func<Type2, Type3>> func2, Type1 input) { return func2(func1(input)) } 

Congratulations, you've invented function composition.

Wrapper functions like this don't have much use when they're specialized to one type. However, if you introduce some type variables and omit the input parameter, then your GetFormattedRate definition looks like this:

public static Func<A, C> Compose( Func<B, C> outer, Func<A, B>> inner) { return (input) => outer(inner(input)) } var GetFormattedRate = Compose(FormatRate, GetRate); var formattedRate = GetFormattedRate(rateKey); 

As it stands, what you're doing has little purpose. It's not generic, so you need to duplicate that code all over the place. It overcomplicates your code because now your code has to assemble everything it needs from a thousand tiny functions on its own. Your heart's in the right place though: you just need to get used to using these sorts of generic higher order functions to put things together. Or, use a good old fashion lambda to turn Func<A, B> and A into Func<B>.

Don't repeat yourself.

Matching types
Source Link
Caleth
  • 12.4k
  • 2
  • 29
  • 45

If you do this long enough, you'll eventually find yourself writing this function over and over:

public static Type3 CombineFunc1AndFunc2( Func<Type1, Type2> func1, Func<Type2, Type3>> func2, stringType1 input) { return func2(func1(input)) } 

Congratulations, you've invented function composition.

Wrapper functions like this don't have much use when they're specialized to one type. However, if you introduce some type variables and omit the input parameter, then your GetFormattedRate definition looks like this:

public static Func<A, C> Compose( Func<B, C> outer, Func<A, B>> inner) { return (input) => func2(func1(input)) } var GetFormattedRate = Compose(FormatRate, GetRate); var formattedRate = GetFormattedRate(rateKey); 

As it stands, what you're doing has little purpose. It's not generic, so you need to duplicate that code all over the place. It overcomplicates your code because now your code has to assemble everything it needs from a thousand tiny functions on its own. Your heart's in the right place though: you just need to get used to using these sorts of generic higher order functions to put things together. Or, use a good old fashion lambda to turn Func<A, B> and A into Func<B>.

Don't repeat yourself.

If you do this long enough, you'll eventually find yourself writing this function over and over:

public static Type3 CombineFunc1AndFunc2( Func<Type1, Type2> func1, Func<Type2, Type3>> func2, string input) { return func2(func1(input)) } 

Congratulations, you've invented function composition.

Wrapper functions like this don't have much use when they're specialized to one type. However, if you introduce some type variables and omit the input parameter, then your GetFormattedRate definition looks like this:

public static Func<A, C> Compose( Func<B, C> outer, Func<A, B>> inner) { return (input) => func2(func1(input)) } var GetFormattedRate = Compose(FormatRate, GetRate); var formattedRate = GetFormattedRate(rateKey); 

As it stands, what you're doing has little purpose. It's not generic, so you need to duplicate that code all over the place. It overcomplicates your code because now your code has to assemble everything it needs from a thousand tiny functions on its own. Your heart's in the right place though: you just need to get used to using these sorts of generic higher order functions to put things together. Or, use a good old fashion lambda to turn Func<A, B> and A into Func<B>.

Don't repeat yourself.

If you do this long enough, you'll eventually find yourself writing this function over and over:

public static Type3 CombineFunc1AndFunc2( Func<Type1, Type2> func1, Func<Type2, Type3>> func2, Type1 input) { return func2(func1(input)) } 

Congratulations, you've invented function composition.

Wrapper functions like this don't have much use when they're specialized to one type. However, if you introduce some type variables and omit the input parameter, then your GetFormattedRate definition looks like this:

public static Func<A, C> Compose( Func<B, C> outer, Func<A, B>> inner) { return (input) => func2(func1(input)) } var GetFormattedRate = Compose(FormatRate, GetRate); var formattedRate = GetFormattedRate(rateKey); 

As it stands, what you're doing has little purpose. It's not generic, so you need to duplicate that code all over the place. It overcomplicates your code because now your code has to assemble everything it needs from a thousand tiny functions on its own. Your heart's in the right place though: you just need to get used to using these sorts of generic higher order functions to put things together. Or, use a good old fashion lambda to turn Func<A, B> and A into Func<B>.

Don't repeat yourself.

Source Link
Jack
  • 4.5k
  • 2
  • 28
  • 31

If you do this long enough, you'll eventually find yourself writing this function over and over:

public static Type3 CombineFunc1AndFunc2( Func<Type1, Type2> func1, Func<Type2, Type3>> func2, string input) { return func2(func1(input)) } 

Congratulations, you've invented function composition.

Wrapper functions like this don't have much use when they're specialized to one type. However, if you introduce some type variables and omit the input parameter, then your GetFormattedRate definition looks like this:

public static Func<A, C> Compose( Func<B, C> outer, Func<A, B>> inner) { return (input) => func2(func1(input)) } var GetFormattedRate = Compose(FormatRate, GetRate); var formattedRate = GetFormattedRate(rateKey); 

As it stands, what you're doing has little purpose. It's not generic, so you need to duplicate that code all over the place. It overcomplicates your code because now your code has to assemble everything it needs from a thousand tiny functions on its own. Your heart's in the right place though: you just need to get used to using these sorts of generic higher order functions to put things together. Or, use a good old fashion lambda to turn Func<A, B> and A into Func<B>.

Don't repeat yourself.