我有一个Enumerable<T>
并且正在寻找一种方法,允许我为每个元素执行一个动作,有点像Select
然后是副作用.就像是:
string[] Names = ...;
Names.each(s => Console.Writeline(s));
Run Code Online (Sandbox Code Playgroud)
要么
Names.each(s => GenHTMLOutput(s));
// (where GenHTMLOutput cannot for some reason receive the enumerable itself as a parameter)
Run Code Online (Sandbox Code Playgroud)
我试过了Select(s=> { Console.WriteLine(s); return s; })
,但它没有打印任何东西.
可枚举性是属性的三个属性之一:可写性,可枚举性和可配置性.我的问题是:
pop
和push
属性是不可枚举的?我有一系列哈希:
[{"Vegetable"=>10}, {"Vegetable"=>5}, {"Dry Goods"=>3>}, {"Dry Goods"=>2}]
Run Code Online (Sandbox Code Playgroud)
我需要在inject
这里使用,但我确实在苦苦挣扎.
我想要一个反映前一个哈希重复键总和的新哈希:
[{"Vegetable"=>15}, {"Dry Goods"=>5}]
Run Code Online (Sandbox Code Playgroud)
我控制输出此哈希的代码,以便我可以在必要时进行修改.结果主要是哈希,因为这可能会最终嵌套任意数量的级别,然后很容易在数组上调用flatten但不会平滑哈希的键/值:
def recipe_pl(parent_percentage=nil)
ingredients.collect do |i|
recipe_total = i.recipe.recipeable.total_cost
recipe_percentage = i.ingredient_cost / recipe_total
if i.ingredientable.is_a?(Purchaseitem)
if parent_percentage.nil?
{i.ingredientable.plclass => recipe_percentage}
else
sub_percentage = recipe_percentage * parent_percentage
{i.ingredientable.plclass => sub_percentage}
end
else
i.ingredientable.recipe_pl(recipe_percentage)
end
end
end
Run Code Online (Sandbox Code Playgroud) 这是我的阵列:
array = [:one,:two,:three]
Run Code Online (Sandbox Code Playgroud)
我想将to_s
方法应用于我的所有数组元素array = ['one','two','three']
.
我该怎么做(将可枚举的每个元素转换为其他元素)?
Ruby中获取第一个可枚举元素的最快方法是什么?
例如:
arr = [12, 88, 107, 500]
arr.select {|num| num > 100 }.first # => 107
Run Code Online (Sandbox Code Playgroud)
我想这样做而不会遍历整个阵列select
,因为我只需要第一场比赛.
我知道我可以做一个each
并且取决于成功,但我认为有一种本地方法可以做到这一点; 我只是没有在文档中找到它.
HttpServletRequest使用了很多java.util.Enumeration.我想在for-each中使用它们,所以我需要将它们转换为可交换的.这不是问题,但由于我有多个项目需要这个,我需要一个库才能做到这一点.我宁愿不做自己的 - 有没有支持这种装饰的标准库?
是否有内置构造将枚举转换为Iterable?
我想知道如何将DataTable转换为Dictionary.我做了这样的事.
using System.Linq;
internal Dictionary<string,object> GetDict(DataTable dt)
{
return dt.AsEnumerable()
.ToDictionary<string, object>(row => row.Field<string>(0),
row => row.Field<object>(1));
}
Run Code Online (Sandbox Code Playgroud)
但我得到:
System.Data.EnumerableRowCollection不包含'ToDictionary'的定义和最佳扩展方法重载'System.Linq.Parallel.Enumerable.ToDictionary(System.Linq.ParallelQuery,System.Func,System.Collections.Generic.IEqualityComrparer)'有一些无效的争论
我该如何解决这个问题?
谢谢
在Ruby的最新版本中,许多方法在没有块的情况下被调用时Enumerable
返回Enumerator
:
[1,2,3,4].map
#=> #<Enumerator: [1, 2, 3, 4]:map>
[1,2,3,4].map { |x| x*2 }
#=> [2, 4, 6, 8]
Run Code Online (Sandbox Code Playgroud)
我想在我自己的方法中做同样的事情,如下:
class Array
def double(&block)
# ???
end
end
arr = [1,2,3,4]
puts "with block: yielding directly"
arr.double { |x| p x }
puts "without block: returning Enumerator"
enum = arr.double
enum.each { |x| p x }
Run Code Online (Sandbox Code Playgroud) 我有一系列的会员资格.每个会员都是一个团体.我需要按照组的名称对这个成员数组进行排序.我尝试了很多不同的方法,最新的方法是:
@memberships.sort_by! { |m| m.group.name }
Run Code Online (Sandbox Code Playgroud)
但是,这并没有按名称排序.它似乎是随机排序数组.
@memberships等于:
[
{
id: 2141,
user_id: 491,
group_id: 271,
member_type: "member",
group: {
id: 271,
name: "Derek's",
privacy: "open",
bio_image_url: "/bio_images/medium/missing.png?1340285189",
member_count: 1,
upcoming_checkins_count: 0
}
},
{
id: 2201,
user_id: 221,
group_id: 291,
member_type: "member",
group: {
id: 291,
name: "Rounded Developement",
privacy: "closed",
bio_image_url: "/groups/medium/291/bioimage.jpg?1340736175",
member_count: 7,
upcoming_checkins_count: 0
}
}
]
Run Code Online (Sandbox Code Playgroud)
注意:这确实有效 - > @ memberships.sort_by!{| m | m.group.id}
它将基于group.id对数组进行排序,所以它可能与按字母顺序排序有关吗?
任何帮助将非常感激.
是否有Rails的类或其他扩展允许超过系列中的前几个元素(以及最后一个)?这些工作:
[2,45,2,14,53,23,634,346,34,46,643,634,346,34,34].fifth
# -> 53
[2,45,2,14,53,23,634,346,34,46,643,634,346,34,34].last
# -> 34
Run Code Online (Sandbox Code Playgroud)
那么在哪里?
list.sixth
list.hundredth
Run Code Online (Sandbox Code Playgroud) enumerable ×10
ruby ×6
arrays ×3
linq ×2
c# ×1
c#-3.0 ×1
collections ×1
datatable ×1
dictionary ×1
hash ×1
java ×1
javascript ×1
key ×1
open-source ×1
prototype ×1
sorting ×1