lambda

Rspec Mocks: mock / yield the block from a method call

I've got this code: Net::SSH.start(@server, @username, :password => @password) do |ssh| output = ssh.exec!(@command) @logger.info 'SSH output: ' @logger.info output end I can mock the SSH.Start using RSpec's mock framework like this, to tell me that I've started the SSH session: Net::SSH.should_receive(:start)....

What does "lambda" mean in Python, and what's the simplest way to use it?

Can you give an example and other examples that show when and when not to use Lambda? My book gives me examples, but they're confusing. ...

Implicit Type Inference only for lambda expressions ? Why ? Confused !

I have the following sample code (used for C# 3.5 study purpose only !). I am making a call to the Sort function that accepts an IEnumerable and a sort function. If I call it using a lambda expression (Case A) the compiler can derive the return type TResult, however when I pass the func SortInt (Case B) the compiler throws an error ! ...

Is there a way to perform "if" in python's lambda

In python 2.6, I want to do: f = lambda x: if x==2 print x else raise Exception() f(2) #should print "2" f(3) #should throw an exception This clearly isn't the syntax. Is it possible to perform an if in lambda and if so how to do it? thanks ...

What's the best way to define & access selected properties in C#?

From my recent question, I try to centralize the domain model by including some silly logic in domain interface. However, I found some problem that need to include or exclude some properties from validating. Basically, I can use expression tree like the following code. Nevertheless, I do not like it because I need to define local variab...

C# - Multiple groupings

I have an IList<SomeObject> where SomeObject has properties for Date (DateTime) and SomeEnum. I've been trying, first with extension functions then with for loops to group first day, they by the value of the enum. I need the output as SomeObjectGroup objects, where the groups have properties for Date, the enum value and a List<SOmeObject...

Working on lambda expression

I am squaring each integer in a List. Here is the code. class SomeIntgs { List<int> newList = new List<int>(); public List<int> get() { IEnumerable<int> intrs = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; newList.AddRange(intrs); return newList; } } I am getting error in Main() SomeIntgs stg =...

Write a method which accepts a lambda expression.

Hi, I have a method with the following signature: void MyMethod(Delegate d){}; void MyMethod(Expression exp){}; void MyMethod(object obj){}; However, this fails to compile: MyMethod((int a) => a) with the following error: "Cannot convert lambda expression to type 'object' because it is not a delegate type" Why doesn't this ...

Ruby: yield block from a block?

Is it possible for a lambda, proc, method or other type of block in ruby, to yield to another block? something like... a = lambda { puts 'in a' yield if block_given? } a.call { puts "in a's block" } this doesn't work... it just produces in a => nil I'm wondering if there is a way to get the block to call a block, though... ...

What does the => operator do?

I am working through a C#/ASP.NET tutorial and I run into an operator i have never seen before. return RSVPs.Any(r => r.AttendeeName.Equals(userName, StringComparison.InvariantCultureIgnoreCase)); what does => mean in this line of code?? ...

Tricky question about generics, inheritance and chaining

For context - read this. Problem: class Program { static void Main() { var b = new bar(); b.buzz().fizz().buzz().fizz(); //cool // ^FAIL!!!...<------------------------------------ Console.ReadLine(); } } public class foo { p...

Lambda Expression

Can I simplify this statement with a lamda expression? var project = from a in accounts from ap in a.AccountProjects where ap.AccountProjectID == accountProjectId select ap; ...

What is wrong with this lambda expression?

I am really struggling to understand why, when I change my code to use a lamdba expression, it doesn't work. This code works and prints on console: object dummy = new object(); InterServer.ExecuteDataReader(new InterServerRequest(ServerID.a_01, "dbo.getbooks") { Params = new Dictionary<string, object> { { "To...

Getting the object out of a MemberExpression?

So, lets say I have the following expression in C#: Expression<Func<string>> expr = () => foo.Bar; How do I pull out a reference to foo? ...

NotSupportedException when using lambda against a DataContext and a generic function constraint of interface

I'm not entirely sure the title is properly worded, but here's the situation... I noticed today while trying to create a generic save function for Linq to Sql that when I use lambda against a data context select. It breaks within a generic function with a type constraint of another generic interface. However, it works fine with LINQ syn...

How to pass Generic Type parameter to lambda expression?

I have a lambda expression which accepts, a int? (nullable integer), which returns value if value exists or DBNull.Value otherwise. Func<int?, object> getId = id => id.HasValue ? id.Value : (object)DBNull.Value; The goal here is that, I want to make that expression slightly a bit more generic so that I can pass any nullable types lik...

Generics and Database Access

I have the following method I can pass in a lambda expression to filter my result and then a callback method that will work on the list of results. This is just one particular table in my system, I will use this construct over and over. How can I build out a generic method, say DBget that takes a Table as a parameter(An ADO.NET dataservi...

Use Optional OR Clause in Linq.Table.Where()

Is there a way to make the ProjectID check below part of an optional block? I'm a recent .Net convert from JEE and I'm looking for something similar to the Hibernate Criteria API. I'd like to simplify the block below and only have to call Where() once. I'm also not sure of the performance implications of doing a Where() with lambdas as I...

How to query a nested list using a lambda expression

In my repository implementation I can run the following query using a lambda expression: public IList<User> GetUsersFromCountry(string) { return _UserRepository.Where(x => x.Country == "Sweden").ToList(); } So far so good, simple stuff. However, I'm having difficulties to write a lambda expression against a neste...

How to return all keys with a certain value from a list of KeyValuePair (vb.net or C#)

Given the following vb.net class: Friend Class PairCollection(Of TKey, TValue) Inherits List(Of KeyValuePair(Of TKey, TValue)) Public Overloads Sub Add(ByVal key As TKey, ByVal value As TValue) Me.Add(New KeyValuePair(Of TKey, TValue)(key, value)) End Sub Public Function FindByValue(ByVal value As TValue) As Li...