I have a view laid out like this:
Text
Spacer
Rectangle
Spacer
I'm trying to make the position of the Rectangle remain constant unless the Text is close enough to push it down. But currently, if the text grows a line taller, the rectangle moves down.
VStack {
Text("Hello")
Spacer()
Rectangle()
.frame(width: 50, height: 50)
Spacer()
}
I tried making the Spacer layoutPriority lower than the Text and Rectangle to no avail.
I'm not sure if I understand the question clearly, but it looks like what you need.
VStack {
Text(
"Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World "
)
.frame(minHeight: 0, maxHeight: .infinity)
Rectangle()
.frame(width: 50, height: 50)
.padding(.vertical, 100)
}
If your view can have a fixed height, you could set a max height for the lower Spacer() and set higher LayoutPriority() for the Text and Rectangle.
Here is an example:
VStack {
Text("Make this text longer and longer")
.layoutPriority(1)
Spacer()
Rectangle()
.frame(width: 50, height: 50)
.layoutPriority(1)
Spacer()
.frame(maxHeight: 120)
}
.frame(width: 100, height: 300)
Now, if you make the text longer it will grow in height and push the rectangle down as soon as the text touches it. I hope it helps.
So I have these two texts that I'm trying to align at the same pixel basically, but it seems that one is more offset than the other. Like so, the one on the bottom seems to be further left than the other.
Here is the code:
VStack(alignment: .leading) {
Text("Enter your phone number")
.padding(15)
.frame(alignment: .leading)
.foregroundColor(Color.white)
.font(Font.custom("URWDIN-Thin", size: 30))
//Subtitle
Text("You will be sent a code for verification")
.padding(15)
.frame(alignment: .leading)
.foregroundColor(Color.white)
.font(Font.custom("URWDIN-Thin", size: 16))
Any idea as to why this may be?
You have chosen a font in which capital ‘E’ has a noticeable left side bearing. Left side bearing is the distance from the “pen position” to the left edge of the tight bounding box of the glyph.
That's why your lines of text look misaligned. Here's my test:
PlaygroundPage.current.setLiveView(
VStack(alignment: .leading) {
Text("Enter your phone number")
.border(Color.red, width: 1)
.font(Font.custom("URWDIN-Thin", size: 30))
Text("You will be sent a code for verification")
.border(Color.red, width: 1)
.font(Font.custom("URWDIN-Thin", size: 16))
Text("Enter your phone number")
.border(Color.red, width: 1)
.font(Font.system(size: 30))
Text("You will be sent a code for verification")
.border(Color.red, width: 1)
.font(Font.system(size: 16))
}
)
Output:
Notice how, in both URW DIN Thin and in the system font (SF Pro), the capital ‘Y’ has a left side bearing of essentially zero: both glyphs touch the left edge of the frame.
The left side bearing of SF Pro's capital ‘E’ is not zero but it's small: a fraction of the vertical stroke width.
But the left side bearing of the capital ‘E’ in DRW UDM Thin is significant. It's about twice the vertical stroke width. That makes it quite noticeable compared to the zero bearing of the capital ‘Y’.
I'm just getting started with Clojure, trying to wrap my brain around functional/immutable programming.
My simple problem is this - I have a map with two values, which I want to transfer from one, to another (inside the same map). Can this be done with a simple function? Or do I have to get into refs and atoms?
e.g.
(def bucket {:volume 100 :rate 10 :poured 0})
How do I move volume to poured at rate?
(defn pour
[bucket]
?
)
; -> {:volume 90 :rate 10 :poured 10}
It's a bit of both. The bucket is a value and a function will take that value and produce a new value that is based on the original (and shared unchanged values):
user> (def bucket {:volume 100 :rate 10 :poured 0})
#'user/bucket
user> (assoc bucket
:volume (- (:volume bucket) (:rate bucket))
:poured (+ (:poured bucket) (:rate bucket)))
{:rate 10, :volume 90, :poured 10}
it leaves the original bucket in the var named bucket
user> bucket
{:rate 10, :volume 100, :poured 0}
so we can define a function on bucket values:
user> (defn pour [bucket]
(assoc bucket
:volume (- (:volume bucket) (:rate bucket))
:poured (+ (:poured bucket) (:rate bucket))))
#'user/pour
user> (pour bucket)
{:rate 10, :volume 90, :poured 10}
user> bucket
{:rate 10, :volume 100, :poured 0}
This will be very useful when we want to express the idea of a bucket "identity". An identity carries values from state to state in a very useful way. I'll use an atom because I want synchronous updates to a single identiy/thing at a time.
user> (def bucket (atom {:volume 100 :rate 10 :poured 0}))
#'user/bucket
user> (swap! bucket pour)
{:rate 10, :volume 90, :poured 10}
user> (swap! bucket pour)
{:rate 10, :volume 80, :poured 20}
user> (swap! bucket pour)
{:rate 10, :volume 70, :poured 30}
and now our bucket is changing over time, and these changes are viable to everyone. It's worth noting that the function that produces the new values is totally independent of the fact that it's being used to change an atom and can be reused elsewhere. Some Clojurians refer to this as "simple"
Often you will want to compute values like "what is the result of pouring the bucket three times, which is nicely expressed by nesting each call to pour inside the next like so:
user> (pour (pour (pour bucket)))
{:rate 10, :volume 70, :poured 30}
This pattern is so common there is a two character macro to express it: ->
user> (-> bucket pour pour pour)
{:rate 10, :volume 70, :poured 30}
In practice I see clojure compose function via nesting and threadding much more often than through the mutable state sytems (atoms, refs, agents, vars)
Can somebody please explain the main differences? I don't have a clear knowledge about these functions in programming for any language.
Some of the basic data structures in programming languages such as C and C++ are stacks and queues.
The stack data structure follows the "First In Last Out" policy (FILO) where the first element inserted or "pushed" into a stack is the last element that is removed or "popped" from the stack.
Similarly, a queue data structure follows a "First In First Out" policy (as in the case of a normal queue when we stand in line at the counter), where the first element is pushed into the queue or "Enqueued" and the same element when it has to be removed from the queue is "Dequeued".
This is quite similar to push and pop in a stack, but the terms enqueue and dequeue avoid confusion as to whether the data structure in use is a stack or a queue.
Class coders has a simple program to demonstrate the enqueue and dequeue process. You could check it out for reference.
http://classcoders.blogspot.in/2012/01/enque-and-deque-in-c.html
Enqueue and Dequeue tend to be operations on a queue, a data structure that does exactly what it sounds like it does.
You enqueue items at one end and dequeue at the other, just like a line of people queuing up for tickets to the latest Taylor Swift concert (I was originally going to say Billy Joel but that would date me severely).
There are variations of queues such as double-ended ones where you can enqueue and dequeue at either end but the vast majority would be the simpler form:
+---+---+---+
enqueue -> | 3 | 2 | 1 | -> dequeue
+---+---+---+
That diagram shows a queue where you've enqueued the numbers 1, 2 and 3 in that order, without yet dequeuing any.
By way of example, here's some Python code that shows a simplistic queue in action, with enqueue and dequeue functions. Were it more serious code, it would be implemented as a class but it should be enough to illustrate the workings:
import random
def enqueue(lst, itm):
lst.append(itm) # Just add item to end of list.
return lst # And return list (for consistency with dequeue).
def dequeue(lst):
itm = lst[0] # Grab the first item in list.
lst = lst[1:] # Change list to remove first item.
return (itm, lst) # Then return item and new list.
# Test harness. Start with empty queue.
myList = []
# Enqueue or dequeue a bit, with latter having probability of 10%.
for _ in range(15):
if random.randint(0, 9) == 0 and len(myList) > 0:
(itm, myList) = dequeue(myList)
print(f"Dequeued {itm} to give {myList}")
else:
itm = 10 * random.randint(1, 9)
myList = enqueue(myList, itm)
print(f"Enqueued {itm} to give {myList}")
# Now dequeue remainder of list.
print("========")
while len(myList) > 0:
(itm, myList) = dequeue(myList)
print(f"Dequeued {itm} to give {myList}")
A sample run of that shows it in operation:
Enqueued 70 to give [70]
Enqueued 20 to give [70, 20]
Enqueued 40 to give [70, 20, 40]
Enqueued 50 to give [70, 20, 40, 50]
Dequeued 70 to give [20, 40, 50]
Enqueued 20 to give [20, 40, 50, 20]
Enqueued 30 to give [20, 40, 50, 20, 30]
Enqueued 20 to give [20, 40, 50, 20, 30, 20]
Enqueued 70 to give [20, 40, 50, 20, 30, 20, 70]
Enqueued 20 to give [20, 40, 50, 20, 30, 20, 70, 20]
Enqueued 20 to give [20, 40, 50, 20, 30, 20, 70, 20, 20]
Dequeued 20 to give [40, 50, 20, 30, 20, 70, 20, 20]
Enqueued 80 to give [40, 50, 20, 30, 20, 70, 20, 20, 80]
Dequeued 40 to give [50, 20, 30, 20, 70, 20, 20, 80]
Enqueued 90 to give [50, 20, 30, 20, 70, 20, 20, 80, 90]
========
Dequeued 50 to give [20, 30, 20, 70, 20, 20, 80, 90]
Dequeued 20 to give [30, 20, 70, 20, 20, 80, 90]
Dequeued 30 to give [20, 70, 20, 20, 80, 90]
Dequeued 20 to give [70, 20, 20, 80, 90]
Dequeued 70 to give [20, 20, 80, 90]
Dequeued 20 to give [20, 80, 90]
Dequeued 20 to give [80, 90]
Dequeued 80 to give [90]
Dequeued 90 to give []
These are terms usually used when describing a "FIFO" queue, that is "first in, first out". This works like a line. You decide to go to the movies. There is a long line to buy tickets, you decide to get into the queue to buy tickets, that is "Enqueue". at some point you are at the front of the line, and you get to buy a ticket, at which point you leave the line, that is "Dequeue".
A queue is a certain 2-sided data structure. You can add new elements on one side, and remove elements from the other side (as opposed to a stack that has only one side). Enqueue means to add an element, dequeue to remove an element. Please have a look here.
Enqueue means to add an element, dequeue to remove an element.
var stackInput= []; // First stack
var stackOutput= []; // Second stack
// For enqueue, just push the item into the first stack
function enqueue(stackInput, item) {
return stackInput.push(item);
}
function dequeue(stackInput, stackOutput) {
// Reverse the stack such that the first element of the output stack is the
// last element of the input stack. After that, pop the top of the output to
// get the first element that was ever pushed into the input stack
if (stackOutput.length <= 0) {
while(stackInput.length > 0) {
var elementToOutput = stackInput.pop();
stackOutput.push(elementToOutput);
}
}
return stackOutput.pop();
}
In my opinion one of the worst chosen word's to describe the process, as it is not related to anything in real-life or similar. In general the word "queue" is very bad as if pronounced, it sounds like the English character "q". See the inefficiency here?
enqueue: to place something into a queue; to add an element to the tail of a queue;
dequeue to take something out of a queue; to remove the first available element from the head of a queue
source: https://www.thefreedictionary.com