[#69892] [Ruby trunk - Feature #11339] [Open] [PATCH] io.c: avoid kwarg parsing in C API — normalperson@...
Issue #11339 has been reported by Eric Wong.
8 messages
2015/07/07
[#69983] Re: [Ruby trunk - Feature #11339] [Open] [PATCH] io.c: avoid kwarg parsing in C API
— Eric Wong <normalperson@...>
2015/07/15
normalperson@yhbt.net wrote:
[#69990] Re: [Ruby trunk - Feature #11339] [Open] [PATCH] io.c: avoid kwarg parsing in C API
— SASADA Koichi <ko1@...>
2015/07/16
On 2015/07/16 4:41, Eric Wong wrote:
[#69995] Re: [Ruby trunk - Feature #11339] [Open] [PATCH] io.c: avoid kwarg parsing in C API
— Eric Wong <normalperson@...>
2015/07/16
SASADA Koichi <ko1@atdot.net> wrote:
[#69984] $SAFE inside an Array — Bertram Scharpf <lists@...>
Hi,
4 messages
2015/07/15
[#70001] [Ruby trunk - Bug #11336] [Open] TestProcess#test_exec_fd_3_redirect failed on Solaris 10 — ngotogenome@...
Issue #11336 has been updated by Naohisa Goto.
4 messages
2015/07/16
[#70005] Re: [Ruby trunk - Bug #11336] [Open] TestProcess#test_exec_fd_3_redirect failed on Solaris 10
— Eric Wong <normalperson@...>
2015/07/16
Sorry, but I think rb_divert_reserved_fd seems a racy fix. I think the
[#70011] [Ruby trunk - Bug #11362] [Open] [PATCH] ensure Process.kill(:STOP, $$) is resumable — normalperson@...
Issue #11362 has been reported by Eric Wong.
3 messages
2015/07/17
[#70016] [Ruby trunk - Bug #11364] [Open] Use smaller buffer for sendmsg — merch-redmine@...
Issue #11364 has been reported by Jeremy Evans.
8 messages
2015/07/17
[#70052] Re: [Ruby trunk - Bug #11364] [Open] Use smaller buffer for sendmsg
— Eric Wong <normalperson@...>
2015/07/20
merch-redmine@jeremyevans.net wrote:
[#70055] Re: [Ruby trunk - Bug #11364] [Open] Use smaller buffer for sendmsg
— Jeremy Evans <code@...>
2015/07/20
On 07/20 10:46, Eric Wong wrote:
[#70056] Re: [Ruby trunk - Bug #11364] [Open] Use smaller buffer for sendmsg
— Eric Wong <normalperson@...>
2015/07/21
Jeremy Evans <code@jeremyevans.net> wrote:
[#70103] [Ruby trunk - Feature #11375] Decreased Object Allocation in Pathname.rb — richard.schneeman@...
Issue #11375 has been updated by Richard Schneeman.
3 messages
2015/07/23
[#70156] [Ruby trunk - Bug #11396] Bad performance in ruby >= 2.2 for Hash with many symbol keys — dunric29a@...
Issue #11396 has been updated by David Unric.
3 messages
2015/07/28
[ruby-core:69906] [CommonRuby - Feature #11262] Make more objects behave like "Functions"
From:
2851820660@...
Date:
2015-07-09 05:10:48 UTC
List:
ruby-core #69906
Issue #11262 has been updated by 11 22.
http://www.software-rating.com/
http://www.smartlogi.com/
http://www.shareorder.com/
http://www.gzs168.com/
http://www.aimooimage.com/
http://www.chinatowngate.net/
http://www.inspiredhypnosis.co.uk/daocplat.html
http://the303plan.com/tibiagoldforsale.html
----------------------------------------
Feature #11262: Make more objects behave like "Functions"
https://bugs.ruby-lang.org/issues/11262#change-53325
* Author: Jörg W Mittag
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
# What is a Function?
In Ruby, we have the `Proc` class to represent objects which are "function-like". But, in true object-oriented / duck-typing fashion, an object doesn't actually have to be an instance of `Proc` in order to be treated as a function, it only needs to respond to `call`. For cases, where a `Proc` instance is absolutely required (mostly, the `&` unary prefix ampersand "make-me-a-block" operator), there is the `to_proc` conversion.
So, in short: if an object wants to be a function, it must respond to `call`, and should also respond to `to_proc`.
There are some objects in Ruby that *could* be seen as functions, but currently don't respond to `call` or `to_proc`:
# `Array` as mapping
An array is a mapping from indices to elements. "Mapping" is just a different word for (partial) function, though! I propose, that `Array` should implement `call` and `to_proc` in the following manner:
class Array
alias_method :call, :[]
def to_proc
method(:call).to_proc
end
end
# `Hash` as mapping
A hash is a mapping from keys to values. I propose, that `Hash` should implement `call` and `to_proc` in the following manner:
class Hash
alias_method :call, :[]
def to_proc
method(:call).to_proc
end
end
Note the duplication here. This suggests maybe refactoring to break out an `Indexable` mixin that is included by both `Array` and `Hash`. However, this is out of scope of this particular proposal.
# `Set` as predicate
A set is a mapping from values to booleans, i.e. a set is the same as its `include?` predicate. This would mean, for example, that I can pass a `Set` as a predicate to methods like `Enumerable#select`. I propose, that `Set` should implement `call` and `to_proc` in the following manner:
require 'set'
class Set
alias_method :call, :include?
def to_proc
method(:call).to_proc
end
end
I believe that these three additions are worthwhile and fairly uncontroversial. They match with the way arrays, maps and especially sets are treated in mathematics and in other programming languages. E.g. in both [Clojure](http://clojure.org/data_structures#Data%20Structures-Maps%20(IPersistentMap)) and [Scala](http://scala-lang.org/api/current/#scala.collection.Seq), arrays, sets and maps are functions and use function application syntax for accessing values. Scala doesn't even have indexing syntax.
Here are some potential use cases:
numbers_to_words = %w[zero one two three four five six seven eight nine ten eleven twelve]
[4, 7, 1, 0, 8].map(&numbers_to_words)
# => ['four', 'seven', 'one', 'zero', 'eight']
allowed_languages = Set[:ruby, :python, :scala, :scheme]
%i[ruby c cplusplus scala java perl].select(&allowed_languages)
# => [:ruby, :scala]
Here is a more "wild" proposal that is much more controversial. I don't actually propose adding this to Ruby, but I will mention it here as food for thought:
# `Class` as factory
If you squint your eyes, tilt your head sideways and look at it juuuuuuust right, a class is a factory for objects. In other words, it is a function from constructor arguments to instances:
class Class
alias_method :call, :new
def to_proc
method(:call).to_proc
end
end
Example:
class Person
def initialize(name)
@name = name
end
end
%w[matz ko1 charlie].map(&Person)
# => [#<Person:0x007fe50ec19698 @name="matz">, #<Person:0x007fe50ec19648 @name="ko1">, #<Person:0x007fe50ec195f8 @name="charlie">]
# Incompatibilities
This proposal conflicts with #10829.
I believe that having `Array`s behave as functions from indices to elements is natural, unsurprising, and well in line with both mathematics and other languages.
# Related
The code duplication encountered here suggests refactoring to extract two new mixins in the Ruby core library:
module Callable
def to_proc
method(:call).to_proc
end
end
module Indexable
alias_method :call, :[]
end
However, this is out of scope for this particular feature request.
--
https://bugs.ruby-lang.org/