Skip to content

Instantly share code, notes, and snippets.

@JessyCatterwaul
Last active December 20, 2015 05:53
Show Gist options
  • Save JessyCatterwaul/b37f3e2bafeeeef3d25c to your computer and use it in GitHub Desktop.
Save JessyCatterwaul/b37f3e2bafeeeef3d25c to your computer and use it in GitHub Desktop.
enum StringPaddingStyle {case Left, Right}
func padStringToLength(
sourceString: String,
destinationCount: Int,
paddingStyle: StringPaddingStyle = .Left,
paddingCharacter: Character = " "
) -> String {
let padCount = destinationCount - sourceString.characters.count,
padString = String(count: padCount, repeatedValue: paddingCharacter)
return paddingStyle == .Left ? padString + sourceString
: sourceString + padString
}
func elongatedToShowRationaleForShortcuts(
sourceString: String,
destinationCount: Int,
paddingStyle: StringPaddingStyle = .Left,
paddingCharacter: Character = " "
) -> String {
let standsAlone = ""
let
standsAlone1 = "",
standsAlone2 = "",
standsAlone3 = ""
let isUsedAgainInThisList = "",
reliesOnIsUsedInThisList = isUsedAgainInThisList,
andThisAlsoRelies = isUsedAgainInThisList
let padCount = destinationCount - sourceString.characters.count,
padString = String(count: padCount, repeatedValue: paddingCharacter)
return {
if paddingStyle == .Left {return padString + sourceString}
else {return sourceString + padString}
}()
}
@JessyCatterwaul
Copy link
Author

"CURRY" is actually the curry emoji, but Gist comments don't support emoji. Actual code is here: https://github.com/Jessy-/Euler/blob/master/Euler_Metal/EulerProblem.swift
I think adding an empty line after the constants upon which others depend is good, unless only one constant is dependent:

let commandBuffer = commandQueue.commandBuffer(),
   commandEncoder = commandBuffer.computeCommandEncoder()

I thought that I had a good example of how I've used more dependent "combined lets"…

compute = {
   let device = MTLCreateSystemDefaultDevice()!,

      commandQueue = device.newCommandQueue(),
      pipelineState = try! device.newComputePipelineStateWithFunction(
         device.newDefaultLibrary()!.newFunctionWithName(functionName)!
      ),
      sumBuffer = device.newBufferWithLength(4, options: .CPUCacheModeDefaultCache)

   return computeCURRY(
      commandQueue: commandQueue,
      pipelineState: pipelineState,
      sumBuffer: sumBuffer
   )
}()

...but I actually do it like this now:

compute = {
   let device = MTLCreateSystemDefaultDevice()!
   return computeCURRY(
      commandQueue: device.newCommandQueue(),
      pipelineState: try! device.newComputePipelineStateWithFunction(
         device.newDefaultLibrary()!.newFunctionWithName(functionName)!
      ),
      sumBuffer: device.newBufferWithLength(4, options: .CPUCacheModeDefaultCache)
   )
}()

There is consistency in how far in those parameter or constant names are indented, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment