Questions tagged [convenience-methods]

Convenience methods are shorthand abstractions which invoke another method by implicitly passing in default argument(s) for a common use case

Convenience methods are intended to encapsulate the verbosity of an existing API into a single expression by aliasing a method call or set of method calls with default arguments to a new method which represents a specific operation.

References

55 questions
218
votes
8 answers

How to normalize a NumPy array to within a certain range?

After doing some processing on an audio or image array, it needs to be normalized within a range before it can be written back to a file. This can be done like so: # Normalize audio channels to between -1.0 and +1.0 audio[:,0] =…
endolith
  • 25,479
  • 34
  • 128
  • 192
150
votes
12 answers

What is the difference between convenience init vs init in swift, explicit examples better

I am having troubles to understand the difference between both, or the purpose of the convenience init.
Chino Pan
  • 1,658
  • 2
  • 9
  • 9
24
votes
6 answers

Simple/Direct/Heredoc way of constructing a HTML string in Java

In python I can construct a HTML string without worrying about escaping special characters like < or " by simply enclosing the string in triple quotes like: html_string = """

My text with "quotes" and…

das_weezul
  • 6,082
  • 2
  • 28
  • 33
22
votes
4 answers

How to return nil in swift in a custom initializer?

I am trying to write a custom initializer in swift. I have something like this: convenience init(datasource:SomeProtocol) { assert(datasource != nil, "Invalid datasource") if(datasource == nil) { return nil } …
zumzum
  • 17,984
  • 26
  • 111
  • 172
21
votes
5 answers

Android: automatically make variables for all IDs in xml

I noticed one of the most tedious parts of Android development is the layout design, even with layout builder. After setting up the graphics, then the layout, making variable associations with the layout elements is very tedious, such as ImageButton…
CQM
  • 42,592
  • 75
  • 224
  • 366
9
votes
2 answers

What is the definition of Convenience Method in regards to Objective C?

In most languages I have dealt with, one something is called a convenience method, it means that the method does some small task that gets done very frequently, and therefore it is more convenient to use said method. In Objective-C does this…
Stefan H
  • 6,635
  • 4
  • 24
  • 35
5
votes
2 answers

How to return a custom object in a swift convenience initializer?

I'm trying to do something like this: public extension UIImage { public convenience init(whatever: Int) { UIGraphicsBeginImageContextWithOptions(...) //... let image = UIGraphicsGetImageFromCurrentContext() …
5
votes
2 answers

What is the use case for convenience initializer?

In swift there is the concept of designated initializer (which is the "normal" constructor we know from other languages, I assume). But there is also something called Convenience Initializer, which I do understand how to write, but the point is lost…
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
4
votes
0 answers

How to refresh a chunk's diff, during `git add -p`

When staging files with git add in patch mode (-p) I sometimes find a mistake that I would like to correct, before keeping on with adding further changes. Many possible options are prompted: confyrm, plunk, split, edit, etc.. But I don't see any way…
Kamafeather
  • 8,663
  • 14
  • 69
  • 99
4
votes
4 answers

Memory Usage on convenience method vs init method

Recently when I looked into iPhone memory management, I tried to compare the convenience method and init method on the same object. For example, I have UIImageView where it displays a downloaded NSData: Convenience method: imageView.image = [UIImage…
4
votes
1 answer

Is there convenient way to perform model validations with Haskell Persistent?

Is there any way to perform custom validations (some kind of hooks) before each update/replace or insert and return a message when validation fails? Just like it can be done in ActiveModel. I could just write a validating function, but I will need…
s9gf4ult
  • 862
  • 6
  • 20
3
votes
2 answers

Create a generic Data initializer for Decodable types using Swift

@objcMembers public class MyResponse: NSObject, Codable { public let id: String? public let context: String? public let results: [MyResult]? } What is the proper way to parse MyResponse from Data in class extension? I tried the…
ArgenBarbie
  • 571
  • 1
  • 4
  • 10
3
votes
2 answers

Temporary Modified Environment during External Process Call from Emacs

Is there a convenient and functional (with-...-like) way of temporary modifying environment variables when using shell-comand or start-process? Thanks in advance, Per
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
3
votes
5 answers

Is there a better way to handle passing in multiple parameters to methods

I find myself doing the following a lot: /** * Redirect to a MVC controller&action * @param controller * @param action */ public void redirect(String controller, String action) { redirect(controller, action, new HashMap
Peeter
  • 9,282
  • 5
  • 36
  • 53
3
votes
3 answers

Swift 3: Convenience Initializer Extending Foundation's 'Timer' Hangs

I am attempting to extend Foundation's Timer class in Swift 3, adding a convenience initializer. But its call to the Foundation-provided initializer never returns. The problem is illustrated in the following trivialized demo, which can be run as a…
Jerry Krinock
  • 4,860
  • 33
  • 39
1
2 3 4